0

I am trying to create a sort of version of Wordle in python (just for practice). I am having difficulty communicating to the player which letters in their guess match (or closely match) the letters in the target word.

I can highlight matches (i.e. where the letter is in the right place) using uppercase, but I don't know how to differentiate between letters which have a match somewhere in the target word and letters which do not appear at all. The relevant code is below:

def compare_words(word,guess):
    W = list(word)# need to turn the strings into list to try to compare each part
    G = list(guess)
    print(W) # printing just to track the two words
    print(G)
    result =[ ] # define an empty list for our results
    for i in range(len(word)):
        if guess[i] == word[i]:
            result.append(guess[i].upper())
        elif guess[i] in word:
            result.append(guess[i])
        else:
            result.append(" ")
    print (result)
    return result
# note, previous functions ensure the length of the "word" and "guess" are the same and are single words without digits           
x = compare_words("slide","slips")        

['s', 'l', 'i', 'd', 'e']
['s', 'l', 'i', 'p', 's']
['S', 'L', 'I', ' ', 's']

As you can see, the direct matches are upper, the other matches are unchanged and the "misses" are left out. This is not what I want, are usually the whole guess is spat back out with font change or colours to indicate the matches.

I have looked into bolding and colours but it all at the point of printing. I need something built into the list itself, but I am unsure if I can do this. Any ideas?

Cheers

martineau
  • 119,623
  • 25
  • 170
  • 301
MWallace
  • 59
  • 6
  • well tbh why do you need to store the lists? You only need to store how many attempts it took them, and display *to the user* which letters were correct and whether they are in the right place. – Esther May 03 '22 at 16:58
  • Yeah, that is sort of true. I want to show them the result of each guess - to inform the next guess, but I suppose I don't need to store the guess (beyond not wiping the print outputs). I guess I can just print the letters of the guess, and use colours to highlight where they match? – MWallace May 03 '22 at 17:07
  • 1
    you for sure could do that – Esther May 03 '22 at 17:41
  • You should be able to embed [ANSI Escape Codes](https://en.wikipedia.org/wiki/ANSI_escape_code) into the strings stored in a list. See [How do I print colored text to the terminal?](https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal) – martineau May 03 '22 at 18:03
  • Thanks guys. I managed to get it working, just using uppercase for the letter and position matches, lowercase for just letter matches and hyphens for blanks. Just converted back to strings for the output of each guess and printed the guess and the result. Worked pretty well. Even included an easy/hard mode setting where you get the first letter. Good fun :) – MWallace May 03 '22 at 22:01

0 Answers0