-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.py
More file actions
81 lines (60 loc) · 2.14 KB
/
RockPaperScissors.py
File metadata and controls
81 lines (60 loc) · 2.14 KB
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
import random
class Game:
def __init__(self):
# get the computer's pick
self.computer_pick = self.get_computer_pick()
# get the user's pick
self.user_pick = self.get_user_pick()
# get the result of the game
self.result = self.get_result()
def get_computer_pick(self):
# get random number among 1, 2 and 3
random_number = random.randint(1, 3)
# possible options
options = {1: "rock", 2: "paper", 3: "scissors"}
# return the value present at random_number
return options[random_number]
def get_user_pick(self):
# infinite while loop
while True:
user_pick = input("Enter rock/paper/scissors: ")
# convert to lowercase
user_pick = user_pick.lower()
# if user_pick is either rock or paper or scissors,
# terminate the loop
if user_pick in ("rock", "paper", "scissors"):
break
else:
print("Wrong input!")
return user_pick
def get_result(self):
# condition for draw
if self.computer_pick == self.user_pick:
return "draw"
# condition for the user to win
elif self.user_pick == "paper" and self.computer_pick == "rock":
return "win"
elif self.user_pick == "rock" and self.computer_pick == "scissors":
return "win"
elif self.user_pick == "scissors" and self.computer_pick == "paper":
return "win"
# in all other conditions, users lose
else:
return "lose"
def print_result(self):
print(f"Computer's pick: {self.computer_pick}")
print(f"Your pick: {self.user_pick}")
print(f"You {self.result}")
# create an object of the Game class
# run game 5 times
# for i in range(5):
# game = Game()
# game.print_result()
# ask user to run the game again
while True:
game = Game()
game.print_result()
play_again = input("Do you want to play again? (y/n): ")
# if user enter any other character other than y, the game ends
if play_again != "y":
break