Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Keep Track of Previous Guesses

00:00 Keep Track of Previous Guesses and Color Them. If you clear the screen between guesses, the game will look cleaner, but your users will also miss some critical information about their previous guesses.

00:12 You’ll therefore keep track of previous guesses and show information about them to the user. To keep track of all your guesses, you’ll use a list. You can initialize the list with five underscores as placeholders for future guesses. Then, as the user makes a guess, you’ll overwrite the placeholder.

00:32 Start by updating main() as seen on-screen. You’ve added guesses as a list containing all of the guesses. Since the list is zero-indexed, you change the range to range(6) so that it runs from zero to five instead of one to six.

00:53 You can then refer to the current guess as guesses[idx] instead of guess.

01:21 Next, you’ll update how you present the user’s guesses. The new function will print all guesses to the screen, using Rich for nice colors and formatting. As you’ll be choosing an appropriate color for each letter, you’ll loop over the letters in each guess. To facilitate this, you change how you categorize each letter, moving away from the set-based logic that you used earlier.

01:42 You’ll replace show_guess() with show_guesses(). First, delete the old show_guess() function and create the show_guesses() function that takes the guesses and the word.

02:01 For each guess, you create a styled string that wraps each letter in a markup block, adding the appropriate color to classify each letter. You loop over the letters in the guess and in the secret word in parallel using zip(). If the letter is correct, then you style it with a green background.

02:19 If the letter is misplaced—i.e., isn’t correct, but it is in the secret word—then you add a yellow background. If the letter is wrong, then you show it on a gray background, here represented with a hexadecimal code of six sixes.

02:34 Finally, you show the placeholder symbols in a dimmed style. You use console.print() so that Rich renders the colors correctly. To make the table of guesses line up nicely, you use justify to center each guess.

02:55 Before you can use your new function to show the user’s guesses, you need to update main() to call it.

03:10 Note that you now show the guesses before getting a new guess from the user. This is necessary because refresh_page() clears the screen of all previous guesses.

03:28 Run the code. If all works as expected, then you should see the guesses line up in nice colors.

03:40 As you play, you’ll note that your basic game_over() now feels a bit out of place. So, in the next section, you’ll give the ending of your game the Rich treatment as well.

Become a Member to join the conversation.