0% found this document useful (0 votes)
6 views1 page

HHW Comp

The document contains code for a hangman game that randomly selects a 5-letter word from a predefined list and allows the user to guess letters with a limited number of incorrect guesses before losing.

Uploaded by

NitrOx Esports
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

HHW Comp

The document contains code for a hangman game that randomly selects a 5-letter word from a predefined list and allows the user to guess letters with a limited number of incorrect guesses before losing.

Uploaded by

NitrOx Esports
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import random

# Predefined list of five-letter words


five_letter_words = [
'apple', 'grape', 'pearl', 'mango', 'lemon', 'chess', 'spade', 'liver',
'brave',
'clash', 'crisp', 'flute', 'giant', 'jelly', 'knock', 'leash', 'magic',
'noble',
'onion', 'patch', 'quill', 'rival', 'smash', 'thorn', 'union', 'valve',
'whale',
'xerox', 'yield', 'zebra'
]

# Choose a random word from the list


chosen_word = random.choice(five_letter_words)
word_length = len(chosen_word)

# Create a display list with underscores


display = ['_'] * word_length

# Track the number of lives


lives = 5

# List to store guessed letters


guessed_letters = []

# Game loop
while lives > 0:
print(f"Lives: {lives}")
print(' '.join(display))

guess = input("Guess a letter: ").lower()

# Check if the letter has already been guessed


if guess in guessed_letters:
print("You have already guessed that letter. Try again.")
continue

guessed_letters.append(guess)

# Check if the guess is correct


if guess in chosen_word:
for i in range(word_length):
if chosen_word[i] == guess:
display[i] = guess
print("Correct guess!")
else:
lives -= 1
print("Wrong guess.")

# Check if the player has won


if '_' not in display:
print("Congratulations! You won!")
print(f"The word was: {chosen_word}")
break
else:
print("Game Over! You ran out of lives.")
print(f"The word was: {chosen_word}")

You might also like