|
| 1 | +import random |
| 2 | + |
| 3 | +def get_computer_choice(): |
| 4 | + choices = ['rock', 'paper', 'scissors'] |
| 5 | + return random.choice(choices) |
| 6 | + |
| 7 | +def determine_winner(user_choice, computer_choice): |
| 8 | + if user_choice == computer_choice: |
| 9 | + return 'draw' |
| 10 | + elif (user_choice == 'rock' and computer_choice == 'scissors') or \ |
| 11 | + (user_choice == 'paper' and computer_choice == 'rock') or \ |
| 12 | + (user_choice == 'scissors' and computer_choice == 'paper'): |
| 13 | + return 'user' |
| 14 | + else: |
| 15 | + return 'computer' |
| 16 | + |
| 17 | +def play_game(): |
| 18 | + print("Welcome to Rock, Paper, Scissors!") |
| 19 | + |
| 20 | + user_score = 0 |
| 21 | + computer_score = 0 |
| 22 | + rounds = int(input("How many rounds would you like to play? ")) |
| 23 | + |
| 24 | + for _ in range(rounds): |
| 25 | + user_choice = input("Enter rock, paper, or scissors: ").lower() |
| 26 | + if user_choice not in ['rock', 'paper', 'scissors']: |
| 27 | + print("Invalid choice, please choose rock, paper, or scissors.") |
| 28 | + continue |
| 29 | + |
| 30 | + computer_choice = get_computer_choice() |
| 31 | + print(f"Computer chose: {computer_choice}") |
| 32 | + |
| 33 | + winner = determine_winner(user_choice, computer_choice) |
| 34 | + |
| 35 | + if winner == 'user': |
| 36 | + print("You win this round!") |
| 37 | + user_score += 1 |
| 38 | + elif winner == 'computer': |
| 39 | + print("Computer wins this round!") |
| 40 | + computer_score += 1 |
| 41 | + else: |
| 42 | + print("This round is a draw!") |
| 43 | + |
| 44 | + print(f"Scores => You: {user_score} | Computer: {computer_score}") |
| 45 | + |
| 46 | + if user_score > computer_score: |
| 47 | + print(f"Congratulations! You won the game with a score of {user_score} to {computer_score}.") |
| 48 | + elif user_score < computer_score: |
| 49 | + print(f"Sorry, you lost the game. Final score is You: {user_score} | Computer: {computer_score}.") |
| 50 | + else: |
| 51 | + print(f"The game is a draw with both scoring {user_score}.") |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + play_game() |
0 commit comments