-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock paper scissors game.py
38 lines (33 loc) · 1.43 KB
/
rock paper scissors game.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
import random
def get_user_choice():
user_choice = input("Choose rock, paper, or scissors: ").lower()
while user_choice not in ['rock', 'paper', 'scissors']:
user_choice = input("Invalid choice. Please choose rock, paper, or scissors: ").lower()
return user_choice
def get_computer_choice():
return random.choice(['rock', 'paper', 'scissors'])
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a 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 "You win!"
else:
return "You lose!"
def play_game():
user_score = 0
computer_score = 0
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"You chose {user_choice}, and the computer chose {computer_choice}.")
result = determine_winner(user_choice, computer_choice)
print(result)
if result == "You won!":
user_score += 1
elif result == "You lose!":
computer_score += 1
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != 'yes':
print(f"Final Score - You: {user_score}, Computer: {computer_score}")
break
play_game()