Search code examples
pythontermcolorwordle-game

Termcolor returning strange string of characters


I'm trying to build my own version of Wordle, and I've gotten stuck here. This part of the code is meant to color the appropriate character green when it matches the position and letter of the secret word, and yellow when it matches the letter, but not the position. Characters which aren't contained in the secret word aren't colored.

from termcolor import colored
secret = "widow"

def color(word):
    if word[0] == secret[0]:
        word = word.replace(word[0], colored(word[0], 'green'))
    if word[0] == secret[1] or secret[2] or secret[3] or secret[4]:
        word = word.replace(word[0], colored(word[0], 'yellow'))
    if word[1] == secret[1]:
        word = word.replace(word[1], colored(word[1], 'green'))
    if word[1] == secret[0] or secret[2] or secret[3] or secret[4]:
        word = word.replace(word[1], colored(word[1], 'yellow'))
    if word[2] == secret[2]:
        word = word.replace(word[2], colored(word[2], 'green'))
    if word[2] == secret[1] or secret[0] or secret[3] or secret[4]:
        word = word.replace(word[2], colored(word[2], 'yellow'))
    if word[3] == secret[3]:
        word = word.replace(word[3], colored(word[3], 'green'))
    if word[3] == secret[1] or secret[2] or secret[0] or secret[4]:
        word = word.replace(word[3], colored(word[3], 'yellow'))
    if word[4] == secret[4]:
        word = word.replace(word[4], colored(word[4], 'green'))
    if word[4] == secret[1] or secret[2] or secret[3] or secret[0]:
        word = word.replace(word[4], colored(word[4], 'yellow'))
    return word

print(color("woiky"))

In this example I'm expecting "woiky" to print with a green "w" (because woiky and widow both start with a "w"), a yellow "i" and a yellow "o" (because "widow" contains both an "i" and "o", but not in those positions), but instead it prints: [33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m0m[33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m32mw[33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m0m[33m[[0m33m[33m[[0m[33m[[0m0m33m[33m[[0m33m[33m[[0m[33m[[0m0m[33m[[0m33m[33m[[0m[33m[[0m0m0m0moiky

And all the "[" characters are yellow.


Solution

  • This should do what you want, in a smarter way. I didn't want to download termcolor, so I provided a substitute.

    #from termcolor import colored
    secret = "widow"
    
    def colored(a,b):
        return( f"<{b}>{a}</{b}>" )
    
    def color(word):
        build = []
        for i,letter in enumerate(word):
            if letter == secret[i]:
                build.append( colored(letter, 'green'))
            elif letter in secret:
                build.append( colored(letter, 'yellow'))
            else:
                build.append( letter )
        return ''.join(build)
    
    print(color("woiky"))