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 could create your own version with minimal effort.

Game Arcade Setup Room

And Python is a convenient language to use to build a Hangman clone.

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.

Hangman Output if you win

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.

Import therandommodule and define a function,get_random_word_from_wordlist(), to pick a random word from the file.

Hangman Output if you lose

Define a list using the rectangular brackets([]).

Use thewithstatement to bring up 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.

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.

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.

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.

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 character is not present decrease the number of chances.

If present, turn the value of found back to the initial value false.

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.