How to make a Hangman game in Python

In this tutorial, we will show you how to make a simple hangman game using Python.

Updated March 29, 2023

Hello and welcome to this beginner’s tutorial on how to make a hangman game in Python. Hangman is a classic game where one player thinks of a word, and the other player tries to guess the word by suggesting letters one at a time. In this tutorial, we will show you how to make a simple hangman game using Python.

Step 1: Choose a Word

The first step in making a hangman game is to choose a word to be guessed. You can use a list of words, a file containing words, or even ask the user to enter a word. Here’s an example:

# Choose a word
word = 'python'

In this code, we have chosen the word ‘python’. You can replace this with any other word you want.

Step 2: Create the Game Board

The game board is the area where the player will see the letters they have guessed and the letters that are still hidden. We will represent the game board as a list of underscores that is the same length as the chosen word. Here’s an example:

# Create the game board
game_board = ['_' for _ in word]

In this code, we use a list comprehension to create a list of underscores that is the same length as the chosen word.

Step 3: Play the Game

Now that we have the word and the game board, we can start playing the game. We will use a loop that asks the player to guess a letter, and then updates the game board if the letter is in the word. We will also keep track of the number of incorrect guesses, and end the game if the player has made too many incorrect guesses. Here’s an example:

# Play the game
max_guesses = 6
incorrect_guesses = 0

while True:
    # Display the game board
    print(' '.join(game_board))

    # Ask the player to guess a letter
    guess = input('Guess a letter: ')

    # Check if the letter is in the word
    if guess in word:
        for i, letter in enumerate(word):
            if letter == guess:
                game_board[i] = guess
    else:
        incorrect_guesses += 1
        print(f'Incorrect! You have {max_guesses - incorrect_guesses} guesses left.')

    # Check if the player has won or lost
    if '_' not in game_board:
        print('Congratulations! You won!')
        break
    elif incorrect_guesses == max_guesses:
        print(f'Game over! The word was {word}.')
        break

In this code, we start a loop that will continue until the player has won or lost the game. Inside the loop, we display the game board, ask the player to guess a letter, and check if the letter is in the word. If the letter is in the word, we update the game board with the guessed letter. If the letter is not in the word, we increment the number of incorrect guesses and display a message to the player. We also check if the player has won or lost the game by checking if all the letters have been guessed correctly or if the player has made too many incorrect guesses.

Conclusion

In this tutorial, we have shown you how to make a simple hangman game using Python programming language. We have covered choosing a word, creating the game board, and playing the game. You can customize the game by adding more words, changing the number of incorrect guesses allowed, or even adding graphics.

Keep practicing and have fun with Python programming!

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!