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.
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.
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.
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.