forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissors.py
36 lines (29 loc) · 1.1 KB
/
rock-paper-scissors.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
import random
def get_user_choice():
choice = input("Enter your choice (rock, paper, scissors): ").lower()
if choice in ['rock', 'paper', 'scissors']:
return choice
else:
print("Invalid choice! Please choose again.")
return get_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 "Computer wins!"
def play_game():
print("Welcome to Rock, Paper, Scissors!")
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"\nYou chose: {user_choice}")
print(f"Computer chose: {computer_choice}")
result = determine_winner(user_choice, computer_choice)
print(result)
if __name__ == "__main__":
play_game()