Write a complete python program to create Hangman Game in Python.
Here is a complete step by step guide to creating a hangman Game in Python.
Step 1: Program to Set up the game
Program to Create Tic Tac Toe Game in Python
The first step is to set up the Hangman game. Here we define the word to be guessed and the number of allowed guesses and initialize the list of correct and incorrect guesses. Add the following code to your Python file
import random
# List of words to choose from
words = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "grape", "lemon", "pear", "peach"]
# Select a random word from the list
word = random.choice(words)
# Initialize variables
allowed_guesses = 6
correct_guesses = []
incorrect_guesses = []
This code imports the random
module and defines a list of words to choose from. It selects a random word from the list and initializes variables for the number of allowed guesses correct guesses and incorrect guesses.
Step 2: Program to Define the game loop
The next step is to define the game loop. Here, we prompt the user for their guess check if the guess is correct and keep track of the number of allowed guesses. Add the following code to your Python file
# Game loop
while True:
# Print the current word with underscores for missing letters
hidden_word = "".join([letter if letter in correct_guesses else "_" for letter in word])
print(hidden_word)
# Prompt the user for their guess
guess = input("Guess a letter: ")
# Check if the guess is correct
if guess in word:
correct_guesses.append(guess)
else:
incorrect_guesses.append(guess)
allowed_guesses -= 1
# Check if the game is over
if allowed_guesses == 0:
print("You lose! The word was", word)
break
if all(letter in correct_guesses for letter in word):
print("You win! The word was", word)
break
This code defines a while
loop that continues until the game is won or lost. In each iteration of the loop we print the current word with underscores for missing letters and prompt the user for their guess. We then check if the guess is correct update the correct and incorrect guesses and decrement the number of allowed guesses. Finally we check if the game is over by either running out of guesses or correctly guessing the word.
Step 3: Program to Play the game
Now we have implemented the game we can play the game by calling the game loop. Add the following code to the end of your Python file
print("Welcome to Hangman!")
while True:
play_again = input("Do you want to play again? (y/n) ")
if play_again.lower() == "y":
word = random.choice(words)
allowed_guesses = 6
correct_guesses = []
incorrect_guesses = []
continue
else:
break
print("Thanks for playing!")
This code start the game by printing a welcome message and prompt the user to play again. If the user choose to play again new word is selected and the game is restarted. Otherwise the game ends with a thank you message.
Conclusion:
In this tutorial we have walked you through the steps to create a Hangman game in Python. We started by setting up the game defining the game loop and playing the game. Hope So you found this tutorial helpful and can use it as a starting point to create your own games in Python.