-
Notifications
You must be signed in to change notification settings - Fork 46
/
Rock-Paper-Scissor.py
71 lines (57 loc) · 2.21 KB
/
Rock-Paper-Scissor.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
import random
# Print multiline instruction
print('Winning rules of the game ROCK PAPER SCISSORS are:\n'
+ "Rock vs Paper -> Paper wins \n"
+ "Rock vs Scissors -> Rock wins \n"
+ "Paper vs Scissors -> Scissors wins \n")
while True:
print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n")
# Take the input from user
choice = int(input("Enter your choice: "))
# Looping until user enters valid input
while choice > 3 or choice < 1:
choice = int(input('Enter a valid choice please : '))
# Initialize value of choice_name variable corresponding to the choice value
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'Paper'
else:
choice_name = 'Scissors'
# Print user choice
print('User choice is:', choice_name)
print("Now it's Computer's Turn...")
# Computer chooses randomly any number among 1, 2, and 3
comp_choice = random.randint(1, 3)
# Initialize value of comp_choice_name variable corresponding to the choice value
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'Paper'
else:
comp_choice_name = 'Scissors'
print("Computer choice is:", comp_choice_name)
print(choice_name, 'vs', comp_choice_name)
# Determine the winner
if choice == comp_choice:
result = "DRAW"
elif (choice == 1 and comp_choice == 2) or (comp_choice == 1 and choice == 2):
result = 'Paper'
elif (choice == 1 and comp_choice == 3) or (comp_choice == 1 and choice == 3):
result = 'Rock'
elif (choice == 2 and comp_choice == 3) or (comp_choice == 2 and choice == 3):
result = 'Scissors'
# Print the result
if result == "DRAW":
print("<== It's a tie! ==>")
elif result == choice_name:
print("<== User wins! ==>")
else:
print("<== Computer wins! ==>")
# Ask if the user wants to play again
print("Do you want to play again? (Y/N)")
ans = input().lower()
if ans == 'n':
break
# After coming out of the while loop, print thanks for playing
print("Thanks for playing!")