|
| 1 | +import sys |
| 2 | +import random |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | + |
| 6 | +def rps(): |
| 7 | + game_count = 0 |
| 8 | + player_wins = 0 |
| 9 | + python_wins = 0 |
| 10 | + |
| 11 | + def play_rps(): |
| 12 | + nonlocal player_wins |
| 13 | + nonlocal python_wins |
| 14 | + |
| 15 | + class RPS(Enum): |
| 16 | + ROCK = 1 |
| 17 | + PAPER = 2 |
| 18 | + SCISSORS = 3 |
| 19 | + |
| 20 | + playerchoice = input( |
| 21 | + "\nEnter... \n1 for Rock,\n2 for Paper, or \n3 for Scissors:\n\n") |
| 22 | + |
| 23 | + if playerchoice not in ["1", "2", "3"]: |
| 24 | + print("You must enter 1, 2, or 3.") |
| 25 | + return play_rps() |
| 26 | + |
| 27 | + player = int(playerchoice) |
| 28 | + |
| 29 | + computerchoice = random.choice("123") |
| 30 | + |
| 31 | + computer = int(computerchoice) |
| 32 | + |
| 33 | + print(f"\nYou chose {str(RPS(player)).replace('RPS.', '').title()}.") |
| 34 | + print( |
| 35 | + f"Python chose {str(RPS(computer)).replace('RPS.', '').title()}.\n" |
| 36 | + ) |
| 37 | + |
| 38 | + def decide_winner(player, computer): |
| 39 | + nonlocal player_wins |
| 40 | + nonlocal python_wins |
| 41 | + if player == 1 and computer == 3: |
| 42 | + player_wins += 1 |
| 43 | + return "π You win!" |
| 44 | + elif player == 2 and computer == 1: |
| 45 | + player_wins += 1 |
| 46 | + return "π You win!" |
| 47 | + elif player == 3 and computer == 2: |
| 48 | + player_wins += 1 |
| 49 | + return "π You win!" |
| 50 | + elif player == computer: |
| 51 | + return "π² Tie game!" |
| 52 | + else: |
| 53 | + python_wins += 1 |
| 54 | + return "π Python wins!" |
| 55 | + |
| 56 | + game_result = decide_winner(player, computer) |
| 57 | + |
| 58 | + print(game_result) |
| 59 | + |
| 60 | + nonlocal game_count |
| 61 | + game_count += 1 |
| 62 | + |
| 63 | + print(f"\nGame count: {str(game_count)}") |
| 64 | + print(f"\nPlayer wins: {str(player_wins)}") |
| 65 | + print(f"\nPython wins: {str(python_wins)}") |
| 66 | + |
| 67 | + print("\nPlay again?") |
| 68 | + |
| 69 | + while True: |
| 70 | + playagain = input("\nY for Yes or \nQ to Quit\n") |
| 71 | + if playagain.lower() not in ["y", "q"]: |
| 72 | + continue |
| 73 | + else: |
| 74 | + break |
| 75 | + |
| 76 | + if playagain.lower() == "y": |
| 77 | + return play_rps() |
| 78 | + else: |
| 79 | + print("\nππππ") |
| 80 | + print("Thank you for playing!\n") |
| 81 | + sys.exit("Bye! π") |
| 82 | + |
| 83 | + return play_rps |
| 84 | + |
| 85 | + |
| 86 | +rock_paper_scissors = rps() |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + rock_paper_scissors() |
0 commit comments