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.
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 ratings0% 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.
# 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}")