import random def get_user_choice(): user_choice = input("Enter your choice (rock, paper, or scissors): ").lower() while user_choice not in ['rock', 'paper', 'scissors']: print("Invalid choice. Please choose rock, paper, or scissors.") user_choice = input("Enter your choice (rock, paper, or scissors): ").lower() return user_choice def get_computer_choice(): return random.choice(['rock', 'paper', 'scissors']) def determine_winner(user_choice, computer_choice): if user_choice == computer_choice: return "It's a tie!" elif (user_choice == 'rock' and computer_choice == 'scissors') or \ (user_choice == 'paper' and computer_choice == 'rock') or \ (user_choice == 'scissors' and computer_choice == 'paper'): return "You win!" else: return "Computer wins!" def play_game(): user_wins = 0 computer_wins = 0 for _ in range(3): user_choice = get_user_choice() computer_choice = get_computer_choice() print(f"You chose: {user_choice}") print(f"Computer chose: {computer_choice}") result = determine_winner(user_choice, computer_choice) print(result) if "You win" in result: user_wins += 1 elif "Computer wins" in result: computer_wins += 1 if user_wins > computer_wins: print("Congratulations! You win the best of three.") elif computer_wins > user_wins: print("Sorry, computer wins the best of three.") else: print("It's a tie in the best of three.") if __name__ == "__main__": print("Welcome to Rock, Paper, Scissors - Best of Three!") play_game()