Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
6
Indexable
# PVE VERSION OF HANGMAN
import random

# Number of attempts
max_attempts = 10

# Get the word to guess from the player
word = input("Enter a word to guess: ").lower()

# Initialize the guessed word with underscores
guessed_word = '_' * len(word)

# Loop through the number of attempts
for attempt in range(max_attempts):
    # Print the current state of the game
    print(f'Guess the word: {guessed_word}')
    
    # Get a guess from the player
    guess = input('Guess a letter: ').lower()
    
    # Check if the guess is in the word
    if guess in word:
        # Update the guessed word with the new letter
        for i in range(len(word)):
            if word[i] == guess:
                guessed_word = guessed_word[:i] + guess + guessed_word[i+1:]
        print(f'Correct! The word now looks like this: {guessed_word}')
    else:
        print('Incorrect!')
    
    # Check if the word has been guessed
    if guessed_word == word:
        print('Congratulations, you guessed the word!')
        break
    
    # Check if the user has run out of attempts
    if attempt == max_attempts - 1:
        print(f'Sorry, you ran out of attempts. The word was {word}.')
Editor is loading...