Are you in the mood to play a game that you’ve written yourself? Well, you’re in the right place. Hangman is a popular word-guessing game and you may create your own version with minimal effort.

This game is a perfect entertainer, familiar from popular TV game shows and series such as Wheel of Fortune, Letterbox, and Party Time. And Python is a convenient language to use to build a Hangman clone.

Hangman Output if you win

How to Play the Hangman Game

Hangman is a well-known game where one player thinks of a word and the other player tries to guess it by suggesting letters with a fixed number of guesses. One player (in this case the program) presents a sequence of dashes representing each letter of the word, along with a character as a starting point. The other player (or user) guesses the characters present in the word, one by one.

If the letter they guess is in the target word, the program fills the dash with the character. Otherwise, the player loses one chance, and the program draws an element of a stick figure as a tally. In a total of seven chances, if the user guesses the word correctly, they win the game. If not, the program completes its drawing of a stick man.

Hangman Output if you lose

you may create a list of your favorite words or download one likeMieliestronk’s list of more than 58,000 English words. You can also explore and build other fun text-based games such as aninteractive quiz gameor atext-based adventure gamein Python.

How to Build the Hangman Game

The source code of this Hangman game, along with the word list file, is present in thisGitHub repositoryand is free to use.

Import therandommodule and define a function,get_random_word_from_wordlist(), to pick a random word from the file. The text file may contain common nouns, or names of places, animals, movies, and more based on your liking. Define a list using the rectangular brackets([]).

Use thewithstatement to open the file and pass the mode as’r’indicating read-only mode. This automatically takes care of closing the file at the end of the block even in cases of errors. If you view thehangman_wordlist.txtfile, you’ll notice there’s one word present on every line, so the file separates each word with a newline character.

Pass the escape character for a newline (\n) to thesplit()function to store each word in the list you defined earlier. Userandom.choice()to return a random word from the list.

Next, define a function,get_some_letters(), that takes the randomly selected word as a parameter. This function will display a sequence of blank dashes (_) and some letters to the user.

Define empty listlettersto store all the characters present in the word. Use thetempvariable to store a string that contains the number of blank dashes equaling the length of the word. Uselist()to convert the string into a list of characters and iterate over it. Useappend()to add the character to the list if not already present.

Userandom.choice()to choose a random character that you are going to present to the user along with the blank dashes. Iterate over the characters of the word usingenumerateto keep a track of the index of each character.

When you find the randomly selected character, replace the blank dash with it. Usejoin()to unify the list of characters into a complete string and return it.

Define a functiondraw_hangman()that takes the number of chances as a parameter. This function provides a figure of the hanging man. As the number of chances keeps on decreasing, so do the chances of survival. When exhausted, the figure is complete and the game finishes.

Declare a function,start_hangman_game(), that defines the main logic of the program. Get a random word by calling theget_random_word_from_wordlist()function and get the sequence to display to the user using theget_some_letters()function.

Set the number of chances to seven and initialize a variable,found, as false. You’ll set this totrueif the guessed letter is present in the word.

Declare a loop that terminates when the user guesses the word correctly or runs out of chances. Begin the game by displaying the sequence, the number of letters in the word, and the chances left. Ask the user to guess a letter and receive it using theinput()function. Validate the user input by checking the length of the character and whether it is an alphabet.

If the input is valid, check if the character is present in the word and replace it using the process seen earlier and update the value of found. If the character is not present decrease the number of chances. If present, turn the value of found back to the initial value false.

If there are no blank dashes left, display that the user won along with the number of guesses taken otherwise display the hangman graphic according to the number of chances left.

Define a function to play the Hangman game. If the user inputs yes call thestart_hangman_game()function otherwise quit the game with the appropriate message. In case of invalid input ask the user to enter again.

The Output of the Hangman Game

You’ll see the following output if you win the game:

You’ll see the following output if you lose the game:

Use Python to Build Games

You can use Python to build command-line games as well as graphical games. Some interesting command-line games you can build using Python include the number guessing game, the Magic 8 Ball game, Mad Libs, Rock/Paper/Scissors, and a text-adventure game.

Some fun graphical games you might build with python include Tic Tac Toe, Maze, Snake, Pacman, Memory, Life. The best part about these games is that you can build them with just the Python Standard Library.