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)
print(blanks)

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
    else:
        strikes += 1
        print(f"You have {strikes} strikes!")
    print(blanks)
    # 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:

Winning Hangman

Losing:

Losing Hangman

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

Leave a Reply

Discover more from PythonAlgos

Subscribe now to keep reading and get access to the full archive.

Continue reading