Welcome to Hangman

This is a simple Hangman game you can play in your console. Let's get started!

Get a random word

word = 'hangman'

Create an array to store the letters of the word

word_letters = [] for letter in word: word_letters.append(letter)

Create an array to store the incorrect guesses

incorrect_guesses = []

Create a variable to store the number of lives the player has

lives = 6

Create a loop that will continue until either the player has won or lost

while lives > 0:

# Print out the word with underscores for unguessed letters
for letter in word_letters:
    if letter in incorrect_guesses:
        print('_')
    else:
        print(letter)

# Ask for a guess
guess = input('Guess a letter: ')

# Check if the guess is correct or incorrect
if guess in word_letters:
    print('Correct!')
else:
    print('Incorrect!')
    incorrect_guesses.append(guess)
    lives -= 1

# Check if the player has won or lost
if '_' not in word_letters:
    print('You win!')
    break

# Print the remaining lives
print('You have', lives, 'lives left.')

if lives == 0: print('You lose! The word was', word)

Play Hangman Online: A Fun Word Guessing Game

原文地址: https://www.cveoy.top/t/topic/lmfM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录