-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.1.rockpaerscissor_cosure.py
82 lines (65 loc) · 2.56 KB
/
11.1.rockpaerscissor_cosure.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
# Rock Paper and Scissor game implementation with user interactive
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
# Below are the constant remain constant
class RPS(Enum):
ROCK = 1
PAPER = 2
SCISSOR = 3
print("Welcome Rock Paper and Scissor Game".center(50,"*"))
playerChoice = input("Enter...\n1 for Rock\n2 for Paper\n3 for Scissor\n\n")
if playerChoice not in ["1","2","3"]:
print("You must enter 1,2 or 3.")
play_rps()
player = int(playerChoice)
computerChoice = random.choice("123")
machineChoice = int(computerChoice)
print("-".center(25,"-"))
# Below are accessing Enum constant and also removing RPS as prefix
print("You chose:" + str(RPS(player)).replace("RPS.", "") + ".")
print("Python chose:" + str(RPS(machineChoice)).replace("RPS.", "") + ".")
print("\n")
print("Result of this game is\n")
def decide_winner(player,machine):
nonlocal player_wins
nonlocal python_wins
if player == 1 and machineChoice == 3:
player_wins += 1
return "🎉🎉🎉 You win!"
elif player == 2 and machineChoice == 1:
player_wins += 1
return "🎉🎉🎉 You win!"
elif player == 3 and machineChoice == 2:
player_wins += 1
return "🎉🎉🎉 You win!"
elif player == machineChoice:
return "😲😲😲 Game Tie"
else:
python_wins += 1
return "🐍🐍🐍 Python wins!"
game_result = decide_winner(player, machineChoice)
print("Game result :" , game_result)
nonlocal game_count
game_count = game_count + 1
print("\n Game Count: ", str(game_count))
print("\n Player wins: ", str(player_wins))
print("\n Python wins: ", str(python_wins))
playagain = input("\n would you like to play again?\nY for Yes\nQ for Quit\n\n")
if playagain.lower() in ["y","Y"]:
play_rps()
else:
print("👋👋👋👋👋 Thanks")
# we can use playagain = False or break
#break
playagain = False
return play_rps
play = rps()
play()