Use a 'WHILE' loop for guessing

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

Welcome to the fifth tutorial for making wordle with python.  Last time we figured out how to make sure that our words showed up in a list like we wanted them to and we had a method for randomly selecting a word.  We settled on using a shortened "Test" list of words for the remaining debugging portion of our series. The short list included:

TEST_WORD_LIST = [
'error',
'apple',
'cruel',
'ionic',
'pizza'
]

This list will be a good testing basis because it includes tricky things like repeated letters.

Now we can start creating the guts of the actual game.  One of the features of the game is that you only get 6 guesses to try to figure out the word.  We can accomplish that aspect of the game using a 'while' loop.

We will pick up where we left off by doing the following:

  1. Convert the word (a string) into a list of letters
  2. Print a prompt to the screen telling the user to make a guess
  3. Create a variable called "guess" which will be our "guess" at the word
  4. Create a counter to keep track of how many guesses.

In fact, I create two counters; One starts at zero and the other starts at 1:

listword = list(word)
print("Guess a 5 letter word! ")
guess = ""
num_guesses = 0
counter = 1

The reasons for doing that are for convenience.  We could use one counter if we desired.

Next we create our 'while' loop:

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

Now we should test our code to see if it does what we want it to do.  At the command line we will run:

>> python Wordle.py

If you've followed everything like above your code should work.  It might not look pretty yet, and it doesn't declare victory if we guess the correct word but that is a detail that we will get to later!  Right now we just want to make sure that we only get 6 guesses, and that aspect of the program works.

Next time we will start to game plan our logic.  There is a necessary order to it because we want 3 possible things to happen:  If a letter is in the correct place in the word we want it to show up as green.  If a letter is in the word but in the wrong place we want it to show up as yellow.  If you guess a word with 4 'r's in it and the word only has 3 'r's then one of the wrongly placed r's should show us as grey colored.  Also if a letter is generally not in the word it should show up as grey.

So once you have your while loop looking pretty we will come back next time to 'game out' our logic or at least the behavior that we want to see in the game.

See you in the next one!

skip_nextStart Comparing Letters in Words
  • 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
    (currently viewing)
  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory