Hangman Game
in python
PROGRAMING FOR AI
FINAL PROJECT
PRESENTED BY:
SP23-bbah-0022 Mehmood Ali
SP23-bbah-0040 Sufiyan
Content Layout
introduction
• What this code is about or our
topic?
• Code we made Code
• Dry run of that code.
• Output of our code. Dry Run
output
INTRODUCTION:
• Hangman is a classic word-guessing game where one player thinks of a
word, phrase, or a hidden word from a predefined list, and the other player
tries to guess it by suggesting letters within a limited number of attempts.
• The code randomly selects a word from a predefined list of words. The
player is then given a certain number of tries to guess the letters in the
word.
• The code initializes a hidden word with underscores representing the letters
to be guessed. The player is prompted to enter a letter guess, and if the
guessed letter is present in the word, the corresponding underscores in the
hidden word are replaced with the correct letter. If the guessed letter is not
present, the player is informed of a wrong guess and the number of tries is
decreased.
• The game continues until the word is fully guessed or the player runs out of
tries. In the end, a message is displayed indicating whether the player won
or lost due to being out of try.
CODING:
import random
# List of words to choose from
words = ['apple', 'banana', 'orange', 'mango', 'grape', 'pineapple']
# Select a random word from the list
word = [Link](words)
# Initialize the guessed word as a list of underscores
guessed_word = ['_'] * len(word)
# Number of tries
tries = 6
# Loop until the word is guessed or no more tries left
while tries > 0 and '_' in guessed_word:
print(' '.join(guessed_word)) # Display the current state of the guessed word
print('Tries left:', tries)
# Ask the user to input a letter
guess = input('Guess a letter: ').lower()
# Check if the guessed letter is in the word
if guess in word:
# Update the guessed word with the correctly guessed letter
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
else:
print('Wrong guess!')
tries -= 1
print('-------------------')
# Check if the player won or lost
if '_' not in guessed_word:
print('Congratulations! You guessed the word:', ''.join(guessed_word))
else:
print('You lost! The word was:', word)
DRY RUN:
1. import random
• Import the random module, which is used to select a random word from a list.
1. # List of words to choose from
words = ['apple', 'banana', 'orange', 'mango', 'grape', 'pineapple’]
• Define a list called words that contains several words to choose from.
1. # Select a random word from the list
word = [Link](words)
• [Link]() function use to select a random word from the words list and assign it to the
variable word.
4. # Initialize the guessed word as a list of underscores
guessed_word = ['_'] * len(word)
• Initialize the guessed_word list with underscores (_) to represent the letters yet to be guessed.
The length of this list is equal to the length of the selected word.
5. # Number of tries
tries = 6
• Set the tries variable to 6, indicating the maximum number of incorrect guesses the player can
make before losing.
DRY RUN:
6. # Loop until the word is guessed or no more tries left
while tries > 0 and '_' in guessed_word:
print(' '.join(guessed_word))
• Enter the main game loop until the word is guessed correctly or the player runs out of tries. The
loop condition checks if tries > 0 (there are remaining tries) and '_' is still present in the
guessed_word list (indicating letters that haven't been guessed yet).
• Inside the loop, print the current state of the guessed_word using the ' '.join(guessed_word)
function. It joins the letters in the guessed_word list with spaces in between.
7. # Display the current state of the guessed word
print('Tries left:', tries)
• Print the number of tries left.
8. # Ask the user to input a letter
guess = input('Guess a letter: ').lower()
• Ask the player to input a letter guess using the input() function. The input is converted to
lowercase using the lower() method.
DRY RUN:
9. # Check if the guessed letter is in the word
if guess in word:
# Update the guessed word with the correctly guessed letter
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
• Check if the guessed letter is in the word using the condition guess in word.
• If the guessed letter is correct:
• Iterate over the indices of the word using for i in range(len(word)).
• If the current character at index i in the word matches the guessed letter, update the
corresponding index i in the guessed_word with the guessed letter.
• This loop ensures that if the guessed letter appears multiple times in the word, all
occurrences are revealed in the guessed_word.
10. else:
print('Wrong guess!')
tries -= 1
DRY RUN:
• If the guessed letter is incorrect:
Print a message indicating the wrong guess.
Decrement the tries variable by 1.
11. # Check if the player won or lost
if '_' not in guessed_word:
print('Congratulations! You guessed the word:', ''.join(guessed_word))
else:
print('You lost! The word was:', word)
• After the main loop ends, check if the player won by checking if there are no underscores ('_') in
the guessed_word. If there are no underscores, print a congratulatory message along with the
correctly guessed word by joining the letters using ''.join(guessed_word).
• If there are still underscores in the guessed_word, it means the player lost. Print a message
indicating the loss and reveal the word.
OUTPUT:
Thank you!