Start Comparing Letters in Words

Published June 29, 2022, 1:11 p.m.

Welcome back!  Last time we created a while loop that looked like the following:

listword = list(word)
print("Guess a 5 letter word! ")
guess = ""
num_guesses = 0
counter = 1
while num_guesses != 6:
    guess = input(f"Guess {counter}: ")
    counter +=1
    num_guesses +=1

Now we want a method for comparing the letters in the guess with the letters in the actual word. 

I propose that we use the 'zip' function in python which will let us zip together any number of lists (in this case we only want to zip 2 lists together).  So first we will need to cast our guess as a list like we did for our word.  Inside our while loop:

while num_guesses != 6:
    guess = input(f"Guess {counter}: ")
    listguess = list(guess)
    counter +=1
    num_guesses +=1

Now we will use some very explicit naming conventions to start a for-loop that will let us compare the letters:

    ...
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            printback(guess_letter, green)
        else:
            printback(guess_letter, grey)

This begins to give us the behavior that we are looking for.  Obviously we need some more logic to get the job done.  In the next tutorial we will take some time to map out the the behaviors that we want to happen so that our game looks like the wordle game.  See you next time!

skip_nextPlan out the Wordle Logic
  • Introduction to PyWordle

  • Get the latest version of Python (3.10.4)

  • Figure out all the 5 letter words (part 1)

  • Figure out all the 5 letter words (part 2)

  • Color letters with colorama!

  • Get words from our word list!

  • Select a Random Wordle

  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words
    (currently viewing)
  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory