Declaring Victory

Published Sept. 20, 2022, 6:03 p.m.

We've gotten our code to do the right things when the right (and/or wrong) letters are present.  Now we would like the progrgram to do a few other things.  First we want to restrict guesses to only be words that are approved dictionary words.  Next we would like the program to know when we won the game!

These steps are both relatively easy to do.  First we will tackle only accepting dictionary words.  We already made a list of all 5 letter words in a previous tutorial.  We still read them in even though we restricted our list of guess words down to just 5 or 6 words.  We need to do 2 things.  First we will create a 'while' loop that continues to request a guess if the guess is not in the wordlist that we already made. 

print("Guess a 5 letter word! ")
guess = ""
num_guesses = 0
counter = 1
while num_guesses != 6:
    while guess not in wordlist:
        guess = input(f"Guess {counter}: ")
    listguess = list(guess)
    ...

Second we will need to redefine our guess to be an empty string near the bottom of our guess loop.  The reason for the second action is that if we do not redefine our 'guess' variable, then python will accept that our guess #1 is still in the wordlist and move on with the logic.  Therefore we must make sure that we redefine our guess at the end of the loop-- I choose to make it an empty string.

    ...
    counter +=1
    num_guesses +=1
    guess = ""

If we do as directed above, the code now balks at non-approved words and only accepts real word guesses.

Our second big action is that we want the program to declare victory when we win.  I find that it is easier to compare strings than to compare lists so after the logic that colors the words I use the following if statement:

    print()
    # print(word_without_cpl)
    if "".join(listword) == "".join(listguess):
        print(f"Whoo Hoo!  You guessed it in {counter} guesses!")
    print()
    counter +=1
    num_guesses +=1
    guess = ""

You will also notice that I got rid of a few print statements.  Those were there for debugging and we believe that the bugs are gone!

One last thing:  Let's undo the forced word selection of the word 'error'.  However, we should still probably keep the TEST_WORD_LIST for now just so the remainder of debugging is not a brain teaser!

Thanks for watching!

  • 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

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory
    (currently viewing)