Hey there, Are you looking for a fun game? Then, you are on the right website, because today I’ll show you a step-by-step guide on How To Make Hangman In Python.
The coding of the Hangman Game In Python is very simple and easy to understand, which is good for beginners who want to learn Python Games.
What is Hangman Game In Python?
In a Python Hangman game, you have to guess the letters (A-Z) that make up the words. If the player guesses the right letter in the word, that letter shows up in the right place.
The person playing the game has to guess the right word until a man is hung, at which point the game is over.
How To Play Python Hangman?
To play hangman game, the system chooses a secret word, and the user tries to guess the word letter by letter. The system knows the secret and writes a string of dashes, one for each letter in the answer. At first, the only thing known about the target word is how long it is.
How To Make Hangman In Python Step-by-Step Guide For Beginners
Here’s the step-by-step guide on how to code hangman in python.
Time needed: 5 minutes
These are the steps on How To Make Hangman In Python.
- Step 1: Import Libraries.
First, Import all the required libraries of the python game.
import random
import time - Step 2: Create User Input To Register User Complete Name.
Next, let’s create a user input to register the complete name of the user.
# Initial Steps to invite in the game:
print("\nWelcome to Hangman game by IT SOURCECODE\n")
This line of code displays the welcome text of the game.
name = input("Enter your name: ")
print("Hello " + name + "! Best of Luck!")
In these lines of code, the system asks the user to input his/her name, and after that the system print or display the name to the console.
time.sleep(2)
print("The game is about to start!\n Let's play Hangman!")
time.sleep(3)
In these lines of code, we create a time sleep function for 2 and 3 seconds and after that, the system displays the text “The game is about to start“. - Step 3: Create a Main Module of Game.
Next, we will create a main module of the game named “main“.
# The parameters we require to execute the game:
def main():
global count
global display
global word
global already_guessed
global length
global play_game
These lines of code are the global variables that use in the game to store data.
words_to_guess = ["january","border","image","film","promise","kids","lungs","doll","rhyme","damage"
,"plants"]
These lines of code are the guessing words to be guessed by the user.
word = random.choice(words_to_guess)
length = len(word)
count = 0
display = '_' * length
already_guessed = []
play_game = ""
These lines of code are the variables that are assigned to its functions. - Step 4: Create a Loop To Ask The User If Want To Play Again.
Next, we will create a loop to ask the user if he/she wants to play again.
def play_loop():
global play_game
This line of code is the declared variable to be used in this module.play_game = input("Do You want to play again? y = yes, n = no \n")
while play_game not in ["y", "n","Y","N"]:
play_game = input("Do You want to play again? y = yes, n = no \n")
if play_game == "y":
main()
elif play_game == "n":
print("Thanks For Playing! We expect you back again!")
exit()
In these lines of code, it will ask the user if he/she wants to play again. If yes, the game will go to the main module if not the program will exit. - Step 5: Create a Hangman Module
Lastly, we will create the actual game for hangman.
def hangman():
global count
global display
global word
global already_guessed
global play_game
limit = 5
guess = input("This is the Hangman Word: " + display + " Enter your guess: \n")
guess = guess.strip()
if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
print("Invalid Input, Try a letter\n")
hangman()
elif guess in word:
already_guessed.extend([guess])
index = word.find(guess)
word = word[:index] + "_" + word[index + 1:]
display = display[:index] + guess + display[index + 1:]
print(display + "\n")
elif guess in already_guessed:
print("Try another letter.\n")
else:
count += 1
if count == 1:
time.sleep(1)
print(" _____ \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 2:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 3:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 4:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " last guess remaining\n")
elif count == 5:
time.sleep(1)
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | /|\ \n"
" | / \ \n"
"__|__\n")
print("Wrong guess. You are hanged!!!\n")
print("The word was:",already_guessed,word)
play_loop()
if word == '_' * length:
print("Congrats! You have guessed the word correctly!")
play_loop()
elif count != limit:
hangman()
Download The Complete and Runnable Source Code
You can visit and download the complete and runnable source code of Hangman Game In Python for free.
Frequently Asked Questions
How does this Python Hangman game pick its words?
The game loads a word list from a text file (usually words.txt) at startup and randomly selects one for each new round using random.choice(). To add your own words, edit the words.txt file (one word per line, lowercase). For a Filipino BSIT defense, you can swap in Tagalog words or programming-term words (e.g. variable, function, recursion) to make the game domain-specific.
What Python version do I need to run this game?
Python 3.8 or newer works. We recommend Python 3.11 or 3.12 because Pygame ships pre-built wheels for those versions (faster pip install, no compilation errors). Python 3.13 may need a slightly older Pygame release because some wheels lag the latest CPython. Verify your version with python –version on Windows or python3 –version on Mac/Linux.
How do I install Pygame for this project?
Run pip install pygame in your terminal (Windows: open Command Prompt, Mac/Linux: open Terminal). On Windows, if pip is not recognized, try python -m pip install pygame instead. On Linux, you may need sudo apt install python3-pygame if pip fails. Verify with python -c “import pygame; print(pygame.version.ver)”.
Can I use this Python game as my BSIT capstone project?
On its own, no, most Philippine BSIT panels expect a full system with users, data, reports, and a real-world problem. A single Pygame game is too narrow for capstone scope. BUT, you can use this game as ONE module inside a larger capstone (e.g. a gamified learning system, a math practice tool for elementary students with this game as the reward layer, or an arcade-style POS for an internet cafe). Pair the game with a Django backend, a database, and analytics for a defensible capstone.
Can I package this game as a standalone .exe to distribute?
Yes, use PyInstaller. Run pip install pyinstaller then pyinstaller –onefile –windowed your_game.py from the project folder. PyInstaller bundles Python and Pygame into a single .exe (Windows) or .app (Mac) that runs without Python installed on the target machine. Include asset files (images, sounds) with –add-data “assets;assets”. Output goes to the dist/ folder.
Where do I get help if the game does not run?
Check the top 3 most common failures: (1) Pygame is not installed correctly, re-run pip install pygame. (2) The game cannot find an asset file (image/sound), make sure you run python from the project folder so relative paths resolve. (3) Wrong Python version, this code expects Python 3.8+; older Python 2.x or 3.6 raises syntax errors. If you still hit a wall, drop a comment on this article with the exact error message, our team monitors comments daily.
Conclusion
We have completely discussed the step-by-step guide on How To Make Hangman In Python.
I hope this simple game tutorial helps you a lot enhance your knowledge of python programming.
