In Python, we can create simple games very quickly. In this article, we will discuss the implementation of the Python Hangman Game.
How is Hangman Game Played?
The hangman game is a multiplayer game. In this game, one player selects a word. Other players have a certain number of guesses to guess the characters in the word. If the players are able to guess the characters in the entire word within certain attempts, they win. Otherwise, they lose.
How to Create Hangman Game in Python?
To create a hangman game in Python, we will use the following steps.
- First, we will ask for the name of the user. We will take the user input using the input() method. After execution, the input() method takes the input from the user and returns a string.
- Next, we will select a word and ask the user to start guessing the characters in the word.
- We will also define the maximum number of attempts the user can take.
- Now, we will use a while loop to repeatedly ask the user to guess the character until the attempts are exhausted.
- Inside the while loop, if the user guesses the correct character. We will include it in the response. Otherwise, we will notify the user that they made a mistake.
- If the user is able to guess all the characters of the word within the maximum number of attempts, they win the game.
- If the user exhausts all their attempts before guessing the entire word, they lose.
Python Code for Hangman Game
Following is a Python script of the classic game “Hangman”. A row of dashes represents the word to guess. If the player guesses a letter in the word, the script writes it in all its correct positions. The player has 10 turns to guess the word. You can easily customize the game by changing the variables.
Make sure that you understand what each line does. All the python statements have been explained using python comments for this reason.
#importing the time module import time #welcoming the user name = input("What is your name? ") print ("Hello, " + name, "Time to play hangman!") #wait for 1 second time.sleep(1) print ("Start guessing...") time.sleep(0.5) #here we set the secret. You can select any word to play with. word = ("secret") #creates an variable with an empty value guesses = '' #determine the number of turns turns = 10 # Create a while loop #check if the turns are more than zero while turns > 0: # make a counter that starts with zero failed = 0 # for every character in secret_word for char in word: # see if the character is in the players guess if char in guesses: # print then out the character print (char,end=""), else: # if not found, print a dash print ("_",end=""), # and increase the failed counter with one failed += 1 # if failed is equal to zero # print You Won if failed == 0: print ("You won") # exit the script break # ask the user go guess a character guess = input("guess a character:") # set the players guess to guesses guesses += guess # if the guess is not found in the secret word if guess not in word: # turns counter decreases with 1 (now 9) turns -= 1 # print wrong print ("Wrong") # how many turns are left print ("You have", + turns, 'more guesses' ) # if the turns are equal to zero if turns == 0: # print "You Lose" print ("You Lose" ) Output:
What is your name? Aditya Hello, Aditya Time to play hangman! Start guessing... ______guess a character:s s_____guess a character:e se__e_guess a character:c sec_e_guess a character:r secre_guess a character:e secre_guess a character:t secretYou wonEnjoy it!!
Conclusion
In this article, we have discussed the implementation of the hangman game in Python. To learn more about python programming, you can read this article on string manipulation. You might also like this article on python if else shorthand.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

