|
| 1 | +import random |
| 2 | + |
| 3 | +choices = ["rock", "paper", "scissors", "lizard", "spock"] |
| 4 | + |
| 5 | +def get_user_choice(): |
| 6 | + user_choice = input("Enter your choice (rock, paper, scissors, lizard, spock): ").lower() |
| 7 | + while user_choice not in choices: |
| 8 | + user_choice = input("Invalid choice. Please enter rock, paper, scissors, lizard, or spock: ").lower() |
| 9 | + return user_choice |
| 10 | + |
| 11 | + |
| 12 | +def get_computer_choice(): |
| 13 | + return random.choice(choices) |
| 14 | + |
| 15 | + |
| 16 | +def determine_winner(user_choice, computer_choice): |
| 17 | + win_conditions = { |
| 18 | + "scissors": ["paper", "lizard"], |
| 19 | + "paper": ["rock", "spock"], |
| 20 | + "rock": ["lizard", "scissors"], |
| 21 | + "lizard": ["spock", "paper"], |
| 22 | + "spock": ["scissors", "rock"] |
| 23 | + } |
| 24 | + |
| 25 | + if user_choice == computer_choice: |
| 26 | + return "tie" |
| 27 | + elif computer_choice in win_conditions[user_choice]: |
| 28 | + return "user" |
| 29 | + else: |
| 30 | + return "computer" |
| 31 | + |
| 32 | + |
| 33 | +def play_game(): |
| 34 | + print("Welcome to Rock, Paper, Scissors, Lizard, Spock!") |
| 35 | + |
| 36 | + while True: |
| 37 | + while True: |
| 38 | + try: |
| 39 | + num_rounds = int(input("Enter the number of rounds you want to play: ")) |
| 40 | + if num_rounds <= 0: |
| 41 | + print("Please enter a positive number.") |
| 42 | + continue |
| 43 | + break |
| 44 | + except ValueError: |
| 45 | + print("Invalid input. Please enter a number.") |
| 46 | + |
| 47 | + user_score = 0 |
| 48 | + computer_score = 0 |
| 49 | + ties = 0 |
| 50 | + |
| 51 | + for round_number in range(1, num_rounds + 1): |
| 52 | + print(f"\nRound {round_number}/{num_rounds}") |
| 53 | + |
| 54 | + user_choice = get_user_choice() |
| 55 | + computer_choice = get_computer_choice() |
| 56 | + |
| 57 | + print(f"\nYou chose: {user_choice}") |
| 58 | + print(f"Computer chose: {computer_choice}") |
| 59 | + |
| 60 | + winner = determine_winner(user_choice, computer_choice) |
| 61 | + |
| 62 | + if winner == "tie": |
| 63 | + print("It's a tie!") |
| 64 | + ties += 1 |
| 65 | + elif winner == "user": |
| 66 | + print("You win!") |
| 67 | + user_score += 1 |
| 68 | + else: |
| 69 | + print("Computer wins!") |
| 70 | + computer_score += 1 |
| 71 | + |
| 72 | + print(f"\nScores:\nUser: {user_score}\nComputer: {computer_score}\nTies: {ties}") |
| 73 | + |
| 74 | + print("\nFinal Scores:") |
| 75 | + print(f"User: {user_score}") |
| 76 | + print(f"Computer: {computer_score}") |
| 77 | + print(f"Ties: {ties}") |
| 78 | + |
| 79 | + while True: |
| 80 | + play_again = input("\nDo you want to play again? (y/n): ").lower() |
| 81 | + if play_again in ["y", "n"]: |
| 82 | + break |
| 83 | + print("Invalid input. Please enter 'y' or 'n'.") |
| 84 | + |
| 85 | + if play_again != "y": |
| 86 | + print("Thanks for playing!") |
| 87 | + break |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + play_game() |
0 commit comments