Creating Hangman in Python

Brock Byrd
3 min readMay 21, 2021

The game Hangman is a simple but fun game that can pass the time. I’m gonna show you how to make an easy hangman-like game, that will greet a user and prompt them to guess a word that has been defined in the script.

First I will start off by importing the time module from Python this is not neccessary, but it gives a good flow to your script run time. Next I will take in the users name and greet the user and then prompt them to start guessing.

import timename = input("What is your name?")print("Hello, " + name + "time to play hangman!")
print("")
time.sleep(1)print("Start guessing...")
time.sleep(0.5)

I’m first taking in a users name as input and greeting them and then calling time.sleep and passing in the alloted time I want to pass before the next line runs in the script. Next thing I will want to do is define a couple of variables, first being the word that I want the user to guess, next being the guesses that the user has guessed, and the amount of “lives” the user can get.

import timename = input("What is your name?")print("Hello, " + name + "time to play hangman!")
print("")
time.sleep(1)print("Start guessing...")
time.sleep(0.5)
word = "secret"
guesses = ''
turns = 6

Next I will want to create a while loop conditional to check if the turns are more than 0. I’ll also want to create a counter that keeps track of how many times the user has guessed wrong. I’ll also need to display either the letters the user has correctly guessed or print a _ to indicate that the letter in that spot in the word has not been guessed.

import timename = input("What is your name?")print("Hello, " + name + "time to play hangman!")
print("")
time.sleep(1)print("Start guessing...")
time.sleep(0.5)
word = "secret"
guesses = ''
turns = 6
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char)
else:
print("_")
failed += 1
if failed == 0:
print("You won")

So first, create the conditional that checks if the user has turns left by checking if the variable turns is greater than 0. Next create a for loop with a if else conditional that will loop through the guesses and check any of the characters in the guesses match any characters inside the hidden word.

The next thing you want to work on is taking in the users input for their either character or word guesses and then store those guesses inside of the guesses variable. Next, you will want to check if the guess is correct or incorrect and act accordingly, like dock the player a turn and say incorrect.

import timename = input("What is your name?")print("Hello, " + name + "time to play hangman!")
print("")
time.sleep(1)print("Start guessing...")
time.sleep(0.5)
word = "secret"
guesses = ''
turns = 6
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char)
else:
print("_")
failed += 1
if failed == 0:
print("You won")
break
guess = input("Guess a character or guess the word:")
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have " + turns + " more guesses")
if turns == 0:
print("You lose")

First thing you want to do is take in the input from the user with a input prompt, then add that guess into guesses for the script to check if the guess is in the word or not. Then with the conditional below the input you will define what to do if the condition is met.

So while the input is wrong, you want to prompt the user that their guess was wrong and that they have a certain amount of turns left. Once the user is out of turns they should be informed and the script should stop running, that is why you check if the turns is equal to 0 to let the user know that they have lost. Do not forget to add the break to the end of the while loop that is checking the amount of turns left to stop the script from running once the condition is no longer true. You now have a working hangman script!

--

--

Brock Byrd

Full Stack Software Engineer | Sports and Fitness