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