-
Notifications
You must be signed in to change notification settings - Fork 785
/
Python code for a Rock, Paper, Scissors game
38 lines (33 loc) · 1.43 KB
/
Python code for a Rock, Paper, Scissors game
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("Enter Rock, Paper, or Scissors: ").strip().lower()
while user_choice not in ["rock", "paper", "scissors"]:
print("Invalid choice. Please enter Rock, Paper, or Scissors.")
user_choice = input("Enter Rock, Paper, or Scissors: ").strip().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":
return "You win!" if computer_choice == "scissors" else "Computer wins!"
elif user_choice == "paper":
return "You win!" if computer_choice == "rock" else "Computer wins!"
elif user_choice == "scissors":
return "You win!" if computer_choice == "paper" else "Computer wins!"
def play_game():
print("Welcome to Rock, Paper, Scissors!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"You chose {user_choice}.")
print(f"Computer chose {computer_choice}.")
result = determine_winner(user_choice, computer_choice)
print(result)
play_again = input("Do you want to play again? (yes/no): ").strip().lower()
if play_again != "yes":
print("Thanks for playing!")
break
if __name__ == "__main__":
play_game()