Creating Hangman Game with Python: Step-by-Step Guide

2 Min Read

Hangman is a classic word-guessing game that has been entertaining people for generations. In this article, we will guide you through creating a simple hangman python code that allows you to play the game in your command-line interface. This project will help you practice your Python skills, including string manipulation, loops, and conditionals.

Building Your Hangman Python Game

Let’s begin by breaking down the basic components of the Hangman game. The game consists of a secret word, a set number of attempts, and the ability for the player to guess individual letters. We will implement these features using Python functions and constructs.

1. Importing Necessary Libraries

First, let’s import the required Python library, random, to select a random word from a list of words. Add the following line at the top of your Python script:

import random

2. Define the Word List

Next, we need to create a list of words for the game to choose from. You can use any words you like, but for this example, we’ll use a small list of animals:

word_list = ["tiger", "lion", "giraffe", "elephant", "kangaroo"]

3. Create a Function to Choose a Random Word

Now that we have our word list, let’s create a function that selects a random word from it. We’ll use the `random.choice()` function from the random library:

def choose_word(words):
    return random.choice(words)

secret_word = choose_word(word_list)

4. Set Up the Game Loop

With the secret word selected, we can now set up the main game loop. We’ll use a while loop that continues until the player either runs out of attempts or correctly guesses the word. We’ll also display the partially guessed word using underscores for unguessed letters:

def display_word(word, guessed_letters):
    return ''.join([letter if letter in guessed_letters else '_' for letter in word])

attempts = 6
guessed_letters = set()

while attempts > 0:
    print(display_word(secret_word, guessed_letters))

    guess = input("Enter your guess: ").lower()
    if guess in secret_word:
        attempts -= 1

    if set(secret_word) <= guessed_letters:
        print("Congratulations! You guessed the word:", secret_word)
    print("Sorry, you ran out of attempts. The word was:", secret_word)

5. Test and Play the Game

With the code above, you should have a fully functional Hangman game! Run your Python script and give it a try. You can expand the word list, adjust the number of attempts, or even modify the display to make the game more challenging or visually appealing.


Creating a Hangman game in Python is a fun and educational way to practice your programming skills. This simple hangman python code should help you understand the basic structure of the game and serve as a foundation for further improvements and customization. Don’t be afraid to experiment with different features or even try implementing other classic games using Python!

If you want to take your Python skills to the next level, check out these additional resources:

By building projects like this Hangman game, you’ll gain a better understanding of Python’s capabilities and improve your coding skills. Happy coding!

Share this Article
Leave a comment