Untitled
unknown
plain_text
2 years ago
2.0 kB
4
Indexable
#PVP VERSION OF HANGMAN import random # List of words words = ['data', 'analysis', 'machine', 'learning', 'classification', 'problem'] # Number of attempts max_attempts = 10 # Initialize the scores player1_score = 0 player2_score = 0 # Loop through the number of rounds while True: # Select a random word word = random.choice(words) # Initialize the guessed word with underscores guessed_word = '_' * len(word) # Print the initial state of the game print(f'Player 1 score: {player1_score}') print(f'Player 2 score: {player2_score}') print(f'Guess the word: {guessed_word}') # Loop through the number of attempts for attempt in range(max_attempts): # Get a guess from the current player if attempt % 2 == 0: player = 'Player 1' else: player = 'Player 2' guess = input(f'{player}, guess a letter: ') # 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(f'Congratulations, {player} guessed the word!') if player == 'Player 1': player1_score += 1 else: player2_score += 1 break # Check if the user has run out of attempts if attempt == max_attempts - 1: print(f'Sorry, nobody guessed the word. The word was {word}.') # Ask the players if they want to play another round play_again = input('Do you want to play another round? (y/n) ') if play_again.lower() == 'n': break
Editor is loading...