Skip to content

Hangman

  • Project
  • 1 hint
  • 7 of 7 in this chapter

Overview

The Hangman Project is a popular project for beginner programmers. The end product is fun to play with, and all you do is build your own hangman game with Python.

  • Accept an input (single character) and check if it is in the name of the movie/game/series that is randomly selected from a list.
  • The game should also display the blank spaces and filled in characters of the name between each character entry by the user.
  • Keep track of the number of failed attempts by the player. If it surpasses a certain number, let’s say 7, then the game should stop.

Here’s an example of functionality:

Text
_ _ _ _ 
_ 
Enter a Character: d   
Sorry! That character isn't in the name!
Strike # 1/7
Enter a Character: as
Enter a Character: a
This character is in the name!
_ a _ _ 
_ 
Enter a Character: e
Sorry! That character isn't in the name!
Strike # 2/7
Enter a Character: i
Sorry! That character isn't in the name!
Strike # 3/7
Enter a Character: o
Sorry! That character isn't in the name!
Strike # 4/7
Enter a Character: u
Sorry! That character isn't in the name!
Strike # 5/7
Enter a Character: d
Sorry! That character isn't in the name!
Strike # 6/7
Enter a Character: c
This character is in the name!
c a _ _ 
_ 
Enter a Character: r
This character is in the name!
c a r _ 
_ 
Enter a Character: s
This character is in the name!
c a r s 
_ 
Enter a Character: 3
This character is in the name!
c a r s 
3 
    
Congratulations! You've Won! The word was:
'cars 3'

You should think of this project as an opportunity to practice your skills with Python.

However, feel free to take a look at our solution to the Hangman Project. It is pretty complex, so don’t worry if you don’t understand something. Just get in touch with us.

Hints

Hint 1h1

d4

Solution

Python
import math
import random

wordbank = ["skyfall 2", "cars 3", "winnie the pooh", "descendants 2", "avengers endgame", "toy story 4", "aladdin", "hobbs and shaw", "godzilla", "john wick", "terminator dark fate", "captain marvel"]

word_id = random.randrange(0,12)
word = wordbank[word_id]
terms = word.split(" ")
display_terms = []
idi = -1
for term in terms:
    char_num = 0
    idi += 1
    new_term = []
    for char in term:
        char_num += 1
        new_term += "_"
    display_terms += [new_term]
def display():
    for term in display_terms:
        statement = ""
        for char in term:
            statement += char + " "
        print(statement)
def check_done():
    done = True
    for term in display_terms:
        for char in term:
            if char == "_":
                done = False
    return done
display()
correct = False
strikes = 0
while correct == False:
    character = input("Enter a Character: ")
    if len(character) > 1:
        "Enter a SINGLE CHARACTER!"
    else:
        term_id = -1
        found = False
        found_id = []
        for term in terms:
            term_id += 1
            idii = -1
            for char in term:
                idii += 1
                if char == character:
                    found = True
                    found_id += [term_id, idii],
        if found:
            print("This character is in the name!")
            for ids in found_id:
                display_terms[ids[0]][ids[1]] = character
            display()
            if check_done():
                print(" ")
                print(" ")
                print(" ")
                print("Congratulations! You've Won! The word was:")
                print("'"+word+"'")
                print(" ")
                print(" ")
                print(" ")
    
                break
        else:
            print("Sorry! That character isn't in the name!")
            strikes += 1
            print ("Strike # " + str(strikes) +"/7" )
            if strikes >= 7:
                print(" ")
                print(" ")
                print("GAME OVER!!! The correct answer was '" + word + "'!")
                print(" ")
                print(" ")
                print(" ")
                print("Try Again by typing 'python3 hangman.py'")
                print(" ")
                print(" ")
                print(" ")
                break
exit()

There are several areas in this where the code can be simplified and made more efficient. We'll leave that to you to figure out. Also, the str() is used to change data from an integer or float type to a string data type. This basically converts the data to text that we can use in strings, print statements, etc. Hopefully you've enjoyed this!

Give the project a try first.