Simple Python Hangman Game

Hangman is a popular game to implement in programming. You can test your knowledge of concepts like string, boolean, and input when creating the game. It’s also a fun project to do because you can play the game with your friends afterward. If you don’t know about the game, you can read more about it here: Hangman. First, you can play around with the interactive console below to see how the game works. Feel free to modify the code, we’ll be going over them.

Hangman Game

In this post, I will walk through the process of writing the game in Python. I’m going to break the game into smaller pieces so we can write function for each piece. This helps with reusing the code and debugging each piece separately. Let’s get started.

Planning out the game

Hangman is a word guessing game at its core. The game selects a random word and user needs to input each letter per turn. The letter is checked against the word. The game then gives user feedback whether he guessed correctly. User has six chances for incorrect guess before he loses, indicated by the complete picture of the hanging man.

Example of Hangman Game

We don’t need to worry about the picture since this is only the implementation of the game. We can use text to indicate guesses user has left. The outline of the game is as followed:

  • Get random word from list
  • Check user input against word
  • Tell user what letters are available
  • Check if word is guessed correctly
  • Looping through the game

Get random word from list

The list of words is either a list we’ve created or a text file. For now, let’s just make a list of random words from the dictionary. We can use the random module to select the word from that list.

Check user input against word

Now we need to display the number of letters in the word to the user. Since this function checks each time a new user input is entered, we can start with an empty string. With letters not guessed yet, we simply add an underscore, “_”, to to the empty string. With a correct guess, we add the correct letter to the string instead of underscore. Remember to add space to next to the underscore since “_ _ _” is easier to read than “___”.

The necessary parameters for this function are the random word  and a list of guesses, user_guesses , user made.

Tell user what letters are available

Once user enters a letter, we need to remove that letter from the alphabet. This helps user see what letters are still available to use. It is very similar to the function above. It checks the letters inside  user_guesses against the alphabet and return a string with available letters.

Check if word is guessed correctly

Now let’s write a function to check if the letters guessed by user match the word. If each string inside  word are also in user_guesses , the function return True and False otherwise.

Looping through the game

Finally, we need to put all the pieces together to write the game function hangman. We need to keep a few points in mind :

  • Variable for the random word
  • List of letters guessed
  • Number of mistakes the user is allowed
  • Terminating condition for the loop

With that in mind, let’s go over how the game is going to play out. First, the game prints a welcome message to the user. Then it chooses a random word from the list and tells the user how long that word is. Afterward, user is given a number of chances and available letters to choose from the alphabet. The user enters a letter and the game tells him whether that letter is correct or not. The game continues until either user guesses the word correctly or runs out of chances.

The only thing we need to look out for is when the user guesses a letter that he already used. If that’s the case, we need to go back to the beginning of the loop and start over. For the terminating condition, we can either use a  break statement to exit the loop or use a flag to set the loop condition to False. Either will work.

Once we combine everything together, the game will work. By having each piece of the program does different thing, we can easily debug the program if we run into a problem. This is simple way I use to write a Hangman game. If you see anything that could be improved, be sure to let me know in the comment below.

Leave a Reply
