Create Hangman Game in Python [with Source Code]

Today, we will learn how to make the hangman game in python. After executing the code in this article, you will be able to build your own hangman game. The code can also be customized as per your choice.

Before we begin, please bookmark rest of the projects in python project series:
1. Library Management System
2. Python Tic Tac Toe
3. Python Snake Game

About the Hangman game

It is a simple word guessing game in which we guess one character at a time. A row of dashes represents the word to be guessed. The player guesses an alphabet.

If the given word contains that alphabet, then the letter is placed at all the places where it occurs in the word. If the letter is not present in the word, the player tries again. The player has a limited number of chances to correctly guess the word.

Python Hangman Project Implementation

We start with the game by printing some initial messages. The module ‘time’ is used to add some pauses in the game using the sleep() function.

#start with the hangman game
import time
print("-----Hangman game-----")
print("Let's play!")
time.sleep(0.5)

We make the word to be guessed as ‘techvidvan’. This can be any word which the player will be guessing in our hangman game. Then we ask the user to start guessing the letters in the word.

#the word to be guessed
word = 'techvidvan'
print("\n-----Guess the letters in the word-----")
time.sleep(0.5)

Whatever letters the player will guess, we will add them in the initially empty string totalGuesses. The maximum number of tries here is taken to be 11. This is customizable and can be any value that you decide to set in your program.

#initial guesses are none
totalGuesses = ' '
tries = 11

The user keeps guessing the alphabets in the secret word till the total number of tries is greater than 0. We take a variable incorrect that keeps track of the total number of failed guesses by the user.

Initially, the value of the variable incorrect is 0. If the letter is guessed correctly by the user, we print the letter, otherwise we print a dash.

For failed guesses, we also have to increment the value of the incorrect variable by 1. If the value of incorrect is 0, the player wins and we print the secret word. If there are no more tries left, the player will lose.

while(tries > 0):
        #variable to count number of incorrect attempts
            incorrect = 0

            for letter in word:     
                        if letter in totalGuesses:
                        #print the correctly guessed letter if found
                                    print(letter)
                        else:
                        #print a dash if letter not in word
                                    print("_")
                                    incorrect += 1

            if incorrect == 0:
                        #player wins
                        print("You Win")

                        print("Word is - ", word)
                        break

            #input again if wrong letter guessed
            guess = input("\nGuess a letter in the word - ")

            #store guessed letters in totalGuesses
            totalGuesses += guess

            #if guess in not present in the word
            if guess not in word:               
                        tries -= 1
                        print("Wrong")

                        #print tries left for the player
                        print("You have ", + tries, 'guesses left')


                        if tries == 0:
                                    print("You Lose")

Conclusion

We have successfully developed python hangman game. Hangman is apopular game and it's implementation in python is a fun and a good project for python beginners.

Do you want to work on real-time python projects?
If yes, please refer: Real-time python projects

happy Coding

46