Super Simple Python: Hangman

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple hangman game in under 25 lines of Python!

For a video version:

Like many of the Super Simple Python posts we’ve covered, Hangman is also reliant on the `random` library. We’ll be using the `random` library to pick a word much like we did with Rock Paper Scissors. We will also be artificially limiting the number of words just like we artificially limited the dice choice in Dice Roll Simulator.

Picking a Word

We’ll start off by importing libraries as we always do. This program only needs the `random` import. Then we’ll make a word list. I’ve picked the Spice Girls to represent our word list – scary, ginger, sporty, baby, and posh. Once we have a word list, we’ll have to pick a word from there. Remember that list indices in Python (and most programming languages) start at 0. Once we’ve picked a word, we print out a set of underscores equal to the length of the word. Obviously in this case, we know the word list and we know how long each word is so it’s easier for us to guess. Alternatively, you could Scrape the Web and get a list of words by copying the text from a web page.

import random
# make a word list
wordlist = ["scary", "ginger", "sporty", "baby", "posh"]
# pick a word and show length
word = wordlist[random.randint(0, 4)]
blanks = "_"*len(word)

Guessing the Word

Once we’ve picked a word, let’s set up guessing the word. Much like in the High Low Guessing Game, we’ll use a while loop. The while loop will run as long as we haven’t made 5 errors and we haven’t guessed the word. If we’ve made too many errors, we lose. If we guess the word, we win.

In the while loop, we start by prompting the user for a guess. If the guessed letter is in the word, we fill out the blank with that letter. Otherwise, we tell the user they have a strike. After each guess, we’ll check to see if there’s any blanks left, and if there are no blanks, the user has guessed the word and we exit the loop.

# set up guesses and is found
strikes = 0
found = False
# take a letter as input
# check if the letter is in the word
while strikes < 5 and not found:
    guess = input("Guess a letter! ")
    # if the guessed letter is in the word, fill it out
    if guess in word:
        index = word.find(guess)
        blanks = blanks[:index] + guess + blanks[index+1:]
    # if the guessed letter isn’t in the word, we get a strike
        strikes += 1
        print(f"You have {strikes} strikes!")
    # if there are no more blanks, we’ve found the word!
    if "_" not in blanks:
        found = True

When we play the game, it should look like one of the images below. Feel free to add a “You Win!” or “You Lose!” message.


Winning Hangman


Losing Hangman

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!


Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount


Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: