Validate Words That the User Guesses
00:00 Validate the Words That the User Guesses. While you won’t check the user’s guesses against the word list, you should do some validation and alert the user if they’re doing something wrong. In this section, you’ll improve the user feedback that you provide when the user makes their guesses. Currently, you are using the code seen on-screen to handle user input to improve the handling of guesses. You’ll start by refactoring this into a separate function.
00:27
First, add guess_word()
to your file.
00:38
The Rich Console
includes an .input()
method that mirrors the input()
function but allows you to add Rich formatting to the input prompt. While you don’t take advantage of this feature, it’s nice to use console
here as well for consistency.
00:52
You include previous_guesses
as a parameter because soon you’ll use it to check that the user isn’t repeating a guess. Before implementing any checks, though, update main()
to call the new function.
01:17
You create a list of previous guesses by only including elements in guesses
that have already been filled in. Then you pass this list to guess_word()
.
01:27
Now use previous_guesses
to check if the user makes the same guess twice. If they do, you’ll want to warn them about this and let them guess again.
01:36
You implement that with the if
test seen on-screen.
01:47
Using the warning
style that you defined earlier, you print a message to the user informing them they’ve already guessed the word. To let the user make a new guess, you call guess_word()
one more time and return that guess.
02:01 As you learned earlier in this course, recursive calls are usually not the best way to create loops in Python, but in this case, it’s quite elegant. The typical downsides aren’t relevant.
02:11 For example, the time it takes to call the function is negligible compared to the time it takes the user to enter their guess. Since all your words are five letters long, you should also check that all the guesses are five letters long.
02:23 You can do this by adding a second conditional.
02:33 This test follows the same structure as the previous one. You check whether there are five letters in the guess, and if not, you print a warning and let the user guess again. Finally, you can guide the users to use only the letters in the English alphabet.
02:48
The if
test is a bit more complicated in this case, as you need to check each letter in the user’s guess.
03:00
The any()
expression checks whether any of the letters in the guess aren’t in ascii_letters
, a built-in list of the lowercase and uppercase letters from A to Z.
03:10
You use the walrus operator inside any()
to collect an example of a character that’s invalid. If there’s an invalid letter in the user’s guess, then you report it with console.print()
as usual and give the user another attempt. The use of the walrus operator inside of any()
is powerful, but it may not be obvious why it works.
03:29 You can learn more about this in this Real Python course.
03:35 Run the game and try to provoke your code by making different kinds of errors. Do you get helpful feedback when you guess four-letter words or include numbers in your guesses? While the core game is the same as before, your program is now more solid and will guide the user if they make any mistakes. You’ve done a great job implementing your Wordle clone, but before ending this course, you’ll tweak your code here and there by smoothing out a few sharp edges.
Become a Member to join the conversation.