Python Programming is a great language for beginners to start coding, and one fun project is building a Hangman game. This word-guessing game where players try to figure out a word by guessing letters. If they guess too many wrong letters, they lose.
Let’s learn how to make this game step by step in Python!
Step 1: Understanding the Game
In the Hangman game:
- A word is chosen at random, and later the player must guess letters one by one.
- If the player guesses correctly, the letter is revealed in the word.
- If they guess wrong, they lose a life. They only have a limited number of lives before the game ends.
Let’s build a simple Hangman game in Python where the player can guess letters.
Step 2: Setting Up the Game
First, we need to pick a word that the player will try to guess. We’ll create a list of words, and the program will pick one at random using Python’s random module.
import random words = [“python”, “hangman”, “programming”, “developer”] word = random.choice(words) |
Next, we need to track the player’s guesses. We’ll create a variable to store the word they are guessing and a list to keep track of the correct letters.
word = random.choice(words) guessed_word = [“_”] * len(word) |
This will create a list like [‘_’, ‘_’, ‘_’, ‘_’] if the word is “python,” where each underscore represents a letter the player hasn’t guessed yet.
Step 3: Creating the Game Loop
Now we need to set up a loop that lets the player keep guessing letters until they either guess the word or run out of lives.
Let’s start by defining the number of lives the player has (e.g., 6 lives) and running a loop that allows the player to guess letters.
lives = 6 while lives > 0 and “_” in guessed_word: guess = input(“Guess a letter: “).lower() if guess in word: for i in range(len(word)): if word[i] == guess: guessed_word[i] = guess else: lives -= 1 print(f“Wrong guess! You have {lives} lives left.”) print(” “.join(guessed_word)) |
Over here the player:
- Guesses a letter.
- If the letter is in the word, it updates the guessed word.
- If the letter is not in the word, the player loses a life.
Step 4: Finishing the Game
Now, we need to add the winning and losing conditions. If the player guesses the entire word before losing all their lives, they win. Otherwise, they lose.
if “_” not in guessed_word: print(“Congratulations! You guessed the word!”) else: print(f“Game over! The word was ‘{word}’.”) |
Step 5: Complete Code
Here’s the complete code for the Hangman game:
import random # List of words words = [“python”, “hangman”, “programming”, “developer”] # Choose a random word word = random.choice(words) guessed_word = [“_”] * len(word) lives = 6 print(“Welcome to Hangman!”) # Game loop while lives > 0 and “_” in guessed_word: guess = input(“Guess a letter: “).lower() if guess in word: for i in range(len(word)): if word[i] == guess: guessed_word[i] = guess else: lives -= 1 print(f“Wrong guess! You have {lives} lives left.”) print(” “.join(guessed_word)) # Check if the player won or lost if “_” not in guessed_word: print(“Congratulations! You guessed the word!”) else: print(f“Game over! The word was ‘{word}’.”) |
Step 6: Making the Game Better
You can improve your Hangman game by adding new features, such as:
- Multiple words: Let the player guess several words in a row.
- Hints: Give the player a hint about the word.
- Categories: Let the player choose a category (e.g., animals, food, etc.) before playing.
- Visual hangman: Draw a simple hangman figure using text or graphics that gets completed as the player loses lives.
Conclusion
The Hangman game is a fun way to learn Python and practice loops, conditionals, and lists. Once you’ve mastered the basics, you can continue to improve the game by adding new features and making it more interactive. Hangman is not only a fun project but also a great introduction to game development in Python.
Jump right on to code camps for kids, where your kids learn to adapt to the digital world of coding through simple and fun learning experiences! Let them join their first trial class today!
Encourage learning everywhere you go and have fun with your coding journey! Happy Coding!