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

Unlock This Lesson

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

Unlock This Lesson

Make Final Changes

00:00 Final Changes. The colors that Rich provides give your users nice clues about which letters they’ve guessed correctly, but it’s not easy to see at a glance which letters the user has already guessed.

00:12 To help them, you’ll add a line showing the status of each letter in the alphabet.

00:27 You already have the necessary information available inside show_guesses(), so you’ll expand the function to show individual letter statuses.

00:50 You use the dictionary letter_status to keep track of the status of each letter. First, you initialize the dictionary with all uppercase letters.

01:02 Then, as you process each letter of every guess, you update letter_status with properly styled letters. Once you’re done, you join all the letters and print them out with their individual styling.

01:24 Putting this information in front of the user makes your game easier and more enjoyable to play. Earlier, you made sure the user wasn’t greeted by an incomprehensible traceback if the word list happened to be empty. As you’ve improved your game, there are fewer possibilities for users to be exposed to Python error messages, but one possibility that still exists is they can hit Control and C to end the game early. You don’t want to disable their ability to break out of the game, but you can make it do so more cleanly.

01:58 When the user types Control + C, Python raises a KeyboardInterrupt. This is an exception that you can catch with a tryexcept block.

02:06 In this case, though, you don’t need to do any special handling of the exception, so you can therefore use contextlib.suppress(). By adding the context manager outside of your main loop, you ensure that Control + C breaks out of that loop and runs the post-processing code.

02:39 Note that you indent the whole main loop inside the suppress() context manager. If a KeyboardInterrupt is raised inside the loop, then control is immediately passed out of the loop, and game_over() is called.

02:52 The effect of this is the game will end after displaying the secret word to the user. This is the final tweak that you’ll make in this course. The complete source code for the finished game is included with the course materials, and the filename is on-screen. In the next section of the course, you’ll take a look back at what you’ve done.

Become a Member to join the conversation.