How to Make a Hangman Game in Python

Learn how to make a hangman game to guess a word in Python using the standard library.
  · 7 min read · Updated mar 2023 · Python Standard Library · Game Development

Welcome! Meet our Python Code Assistant, your new coding buddy. Why wait? Start exploring now!

One of the most popular basic projects for new programmers is the game called "Hangman". It is a word-guessing game where for every mistake guess the player has, a figure of a man will be drawn hanging in the gallow. This is a pretty cool project as an application of the basics you just learned.

In this tutorial, we'll find out how to create a simple hangman game using Python from scratch with the basic use of some built-in modules. With that being said, let's get started.

Table of Contents

Fetching Game Words

Let's start by getting words for the hangman. I looked on some sites on the internet to get the words that are used for the hangman game, copy them into a text file, and named it words.txt. You can access mine here.

Making Hangman Class

Create a Python file named hangman.py and inside it, import os, random, and ascii_letters from the string module. Below it, create the Hangman() class:

# /*hangman.py

from string import ascii_letters
import os
import random

class Hangman:
    
    def greet_user(self):
        print("Hangman\n")

The __init__() function

Above our greet_user() function, let's add the __init__() function in our class. The first things we're adding are the secret_word and the guessed_word where we access the words.txt, pick a secret word and pass it to secret_word while the guessed_word acts as the letter blanks of the word the player is guessing (the secret word).

class Hangman:

    def __init__(self):
        with open("./words.txt", "r") as file:
            words = file.read().split("\n")
            self.secret_word = random.choice(words)
            self.guessed_word = "*" * len(self.secret_word)

    def greet_user(self):
        print("Hangman\n")

We're also adding the number of incorrect_guess_limit (which is 6 since the stickman has 6 body parts), the number of incorrect_guesses, letter list of wrong_guesses, gallow_pieces for the gallow, and the man_pieces:

class Hangman:

    def __init__(self):
        with open("./words.txt", "r") as file:
            words = file.read().split("\n")
            self.secret_word = random.choice(words)
            self.guessed_word = "*" * len(self.secret_word)

        self.incorrect_guess_limit = 6
        self.incorrect_guesses = 0
        self.wrong_guesses = []
        self.gallow_pieces = [
            "------",
            "|    |",
            "|    ",
            "|  ",
            "|   ",
            "|"
        ]
        self.gallow = "\n".join(self.gallow_pieces)
        self.man_pieces = [
            " \\",
            "/",
            " \\",
            " |",
            "/",
            "O",
        ]
    
    def greet_user(self):
        print("Hangman\n")

Making Functions for Taking a Guess

Let's create first a function that prints all of the player's wrong letter guesses. At the beginning of the game, this will be empty and will be filled as long as the player starts to have error guesses. It helps the player to see what letters are eliminated already and to avoid the player wasting a move to a letter that is already crossed out of the possible letters:

    def show_list_of_wrong_guesses(self):
        print(f"Wrong guesses: {', '.join(self.wrong_guesses)}\n\n")

The next function we're creating is a function that takes letter guesses. It also checks if the given guess is a letter and not a number or a symbol. If the guess value is a letter, it will return the letter and if not, it prints the "Invalid input" message and asks the player again to enter a letter guess. Remember earlier, we imported the ascii_letters and we're gonna use it to check the character sent by the player:

    def take_guess(self) -> str:
        while True:
            guess = input("Guess a letter:\n>>> ")
            if len(guess) == 1 and guess in ascii_letters:
                break
            else:
                print("Invalid input")
        return guess

Let's also create another function for checking if the player already hit the incorrect guess limit:

    def is_out_of_guesses(self) -> bool:
        return self.incorrect_guesses == self.incorrect_guess_limit

Checking the Letters Guessed

Let's create another function inside the class that checks the letter given from the take_guess() function.

    def check_guess(self, guess_letter: str):
        if guess_letter in self.secret_word:
            self._correct_guess(guess_letter)
        else:
            self._wrong_guess(guess_letter)

We need two more functions for listing the guess value to correct guesses or wrong guesses.

Let's have first a function named _correct_guess() which takes the guess_letter as an argument.

    def _correct_guess(self, guess_letter: str):
        index_positions = [index for index, item in enumerate(self.secret_word) if item == guess_letter]
        for i in index_positions:
            self.guessed_word = self.guessed_word[0:i] + guess_letter + self.guessed_word[i+1:]

The index_positions checks each letter on the self.secret_word one by one (enumerating the items and indices) and every time the item has the same value with guess_letter, the item's index will be listed in the index_positions list. It is because some words contain the same letter two or more times, words such as 'door' which has 2 'o's, 'teeth' which has 2 't's and 2 'e's, and 'division' which has 3 'i's.

For every index we have in index_positions we'll add the guess_letter on the self.guessed_word positioned by the said index, and add the other blank characters ahead of that index. Let's say our secret word is 'division'. The value of the self.guessed_word will be:

self.guessed_word = "********"

Let's say the player enters a letter guess 'i'. Its new value will be:

self.guessed_word = "*i*i*i**"

Now, create another function and name it _wrong_guess which also takes the guess_letter as an argument:

    def _wrong_guess(self, guess_letter: str):
        row = 2
        if self.incorrect_guesses > 0 and self.incorrect_guesses < 4:
            row = 3
        elif self.incorrect_guesses >= 4:
            row = 4

        self.gallow_pieces[row] = self.gallow_pieces[row] + self.man_pieces.pop()
        self.gallow = "\n".join(self.gallow_pieces)

        if guess_letter not in self.wrong_guesses:
            self.wrong_guesses.append(guess_letter)
        self.incorrect_guesses += 1

The first row variable tells the row in the gallow in which a "piece of the man" will be placed when the user guessed the wrong letter. This is how each of the man's pieces will be drawn to the gallow.

For the first mistake, it appends the "man's head" at row 3 (index 2) in the self.gallow_pieces array. The second row variable is for the 2nd, 3rd, and 4th wrong guesses, where the left arm, torso, and right arm will be appended at row 4 (index 3) in the self.gallow_pieces array. And the third row variable is for the 5th and 6th mistakes, the left and right legs will be appended at row 5 (index 4) in the self.gallow_pieces array.

The self.man_pieces array contains the "man pieces" to be appended to the "gallow pieces". The self.man_pieces is arranged in reverse order so calling the .pop() method takes each piece of the man (from head to legs) in proper order which will be appended in the gallow.

After a body part was appended, we'll add the guess_letter in the self.wrong_guesses and increase self.incorrect_guesses by 1.

Related: How to Make a Drawing Program in Python.

Running the Game with main()

The last thing we're adding is the main() function that runs the whole game. This function controls the rules of the game:

def main():
    hangman = Hangman()
    while True:
        # greet user and explain mechanics
        os.system('cls' if os.name=='nt' else 'clear')
        hangman.greet_user()
        # show gallow and the hidden word
        print(hangman.gallow, "\n")
        print("Secret word: ", hangman.guessed_word)
        # show the list of wrong guesses
        hangman.show_list_of_wrong_guesses()
        if hangman.is_out_of_guesses():
            print(f"Secret word is: {hangman.secret_word}")
            print("You lost")
            break
        elif hangman.guessed_word == hangman.secret_word:
            print("YOU WIN!!!")
            break
        else:
            # take user guess
            guess = hangman.take_guess()
            # check guess
            hangman.check_guess(guess)

if __name__ == "__main__":
    main()

And we're done! You can now play hangman in your terminal by heading to the program directory and running python hangman.py. Below are some of the game snapshots:

Running the game:

running the game

Invalid character guess:

invalid character guessPlaying the game:

playing the gameCorrectly guessed the mystery word:

correctly guessed the mystery wordNow we know how to create the Hangman game using Python basics with the help of built-in modules. We can now have fun playing and improve our word-guessing ability with this game. I hope you liked this tutorial. Thanks for learning with me and have a great journey!

You can get the complete code here.

Learn also: How to Make a Simple Math Quiz Game in Python

Happy coding ♥

Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!

View Full Code Switch My Framework
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!