This classic game asks players to guess words. Each letter in the word is represented by a blank space. If you guess a letter in the word, the blank at that location is replaced by the letter. If you guess wrong, a figure is drawn. If you make too many wrong guesses, you lose.

This flowchart represents how the game play happens. It is much easier to code a program when you know what should happen. (We’ll talk more about program planning in a later lesson.)

The program below is outlined for you. The instructions will walk you through the game in the order of the flowchart, filling in the outline to make your program work.

Most programs consist of a main() function and several additional functions. The main() function calls the other functions, and at the very end of the program there’s a call to the main() function itself. We are going to use this structure for our Hangman game, except we’re going to call our main() function hangman().

One last note: it might be tempting to copy–paste code from the instructions directly into your program, but I strongly suggest typing your own code (except where you’re told to copy-paste) for two reasons:

  1. The code snippets in the instructions don’t have proper indentations, so pasting them into your program will cause syntax issues.
  2. Typing the code yourself helps you learn and understand it better than just copying and pasting.

Ready to get started?

 

Bonus Features

You have a few options for expanding your program. Of course, you can start by adding a few more words to your word list. But you can also add some extra capabilities to your hangman game!

You may have noticed that the flowchart has an "Ask player to play again" box, but we didn't talk about that. Now's your chance.

In order to replay the game, we'll drop the hangman() function into a while loop.

while True:
 hangman()

This will play the game over and over on and endless loop. Let's give the player a chance to decide whether or not to keep playing (this code will go immediately after the code above):

 again = input("Do you want to play again? Y or N:")
 if again == "N":
 break

If the player enters Y, the loop can continue: we don't need to do anything. If they enter N, we use the keyword break to exit the loop.

As with any input, this might run into issues if the player enters something unexpected. We can add a few features to make this part more robust:

 again = input("Do you want to play again? Y or N:").upper()
 while again[0] not in ["Y", "N"]:
 again = input("Please enter Y for yes or N for no.").upper()
 if again[0] == "N":
 break

There are three main changes here to avoid issues:

  1. The .upper() changes a lowercase y or n to an uppercase Y or N, so that the program can interpret the input in a case-insensitive way.
  2. Using again[0] means that if they typed "yes" or "no", the program will only look at the first letter.
  3. The while loop continues asking the player for input until they enter something valid.

Now, any of the following inputs will be interpreted as "no" and exit the game: N, n, no, No, NO!, nope, nah, nooooo, etc. Anything that starts with an N/n will do. Any answer that starts with a Y/y will start a new game.

Wouldn't it be more interesting if you could include secret phrases instead of just words? Or have words with apostrophes or other punctuation? Let's add these capabilities.

We're not going to make the player guess spaces and punctuation, so we want to add these as "freebies" to the string of correct letters. That way they'll display when we run the displayBoard() function, and they player won't have to guess them to win. Add this code to your hangman() function, right after you initialize the correctLetters variable:

for character in secretWord:
 if character not in 'abcdefghijklmnopqrstuvwxyz':
 correctLetters += character

(Make sure your indentation is correct!)

This code runs through all the characters in secretWord and adds any that aren't in the alphabet to correctLetters.

Now you can add some contractions, phrases, exclamation marks, and other fun stuff to your WORDS list! Note: if you include an apostrophe, make sure you use double quotes " around the string instead of single quotes.

Don't forget, though, that you still can't use capital letters. Perhaps that's another fun feature to add…