-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRock Paper Scissors Game.py
More file actions
72 lines (63 loc) · 2.09 KB
/
Rock Paper Scissors Game.py
File metadata and controls
72 lines (63 loc) · 2.09 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
import random
def display_rules():
print("Rock vs paper->paper wins")
print("Rock vs scissors->Rock wins")
print("Paper vs scissors->scissors wins")
def get_user_choice():
valid_choices = ['r', 'p', 's']
while True:
user_input = input("Choose Rock (r), Paper (p), or Scissors (s): ").lower()
if user_input in valid_choices:
return user_input
else:
print("Invalid input. Please try again.")
def get_computer_choice():
choices = ['r', 'p', 's']
return random.choice(choices)
def determine_winner(user, computer):
if user == computer:
return "tie"
elif (user == 'r' and computer == 's') or \
(user == 'p' and computer == 'r') or \
(user == 's' and computer == 'p'):
return "user"
else:
return "computer"
def display_winner(winner, user, computer):
if winner == "tie":
print("It's a tie!")
elif winner == "user":
print(f"You win! {get_choice_name(user)} beats {get_choice_name(computer)}")
else:
print(f"Computer wins! {get_choice_name(computer)} beats {get_choice_name(user)}")
def get_choice_name(choice):
if choice == 'r':
return "Rock"
elif choice == 'p':
return "Paper"
else:
return "Scissors"
def update_score(winner, user_score, computer_score):
if winner == "user":
user_score += 1
else:
computer_score += 1
return user_score, computer_score
def main():
display_rules()
user_score = 0
computer_score = 0
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
winner = determine_winner(user_choice, computer_choice)
display_winner(winner, user_choice, computer_choice)
user_score, computer_score = update_score(winner, user_score, computer_score)
print(f"Score: User {user_score} - Computer {computer_score}")
print("Do you want to play again? (Y/N)")
ans = input().lower()
if ans != 'y':
break
print("Thanks for playing!")
if __name__ == "__main__":
main()