-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRock Paper Scissors Game.py
More file actions
59 lines (50 loc) · 1.89 KB
/
Rock Paper Scissors Game.py
File metadata and controls
59 lines (50 loc) · 1.89 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
import random
def get_computer_choice():
choices = ['rock', 'paper', 'scissors']
return random.choice(choices)
def get_user_choice():
while True:
user_choice = input("Choose rock, paper, or scissors: ").strip().lower()
if user_choice in ['rock', 'paper', 'scissors']:
return user_choice
else:
print("Invalid input. Please try again.")
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return 'tie'
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'scissors' and computer_choice == 'paper') or \
(user_choice == 'paper' and computer_choice == 'rock'):
return 'user'
else:
return 'computer'
def display_result(user_choice, computer_choice, winner):
if winner == 'tie':
print(f"It's a tie! Both you and the computer chose {user_choice}.")
elif winner == 'user':
print(f"You win! {user_choice} beats {computer_choice}!")
else:
print(f"You lose! {computer_choice} beats {user_choice}!")
def main():
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_result(user_choice, computer_choice, winner)
if winner == 'user':
user_score += 1
elif winner == 'computer':
computer_score += 1
print(f"Score: User {user_score} - Computer {computer_score}")
while True:
play_again = input("Do you want to play again? (yes/no): ").strip().lower()
if play_again in ['yes', 'no']:
break
else:
print("Invalid input. Please enter 'yes' or 'no'.")
if play_again == 'no':
break
if __name__ == '__main__':
main()