-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 3
More file actions
53 lines (43 loc) · 1.67 KB
/
Copy pathTask 3
File metadata and controls
53 lines (43 loc) · 1.67 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
import random
def get_user_choice():
"""
Function to get user's choice: 'rock', 'paper' or 'scissors'
"""
user_choice = input("Enter your choice (rock/paper/scissors): ").strip().lower()
while user_choice not in ['rock', 'paper', 'scissors']:
print("Invalid choice. Please enter either 'rock', 'paper', or 'scissors'.")
user_choice = input("Enter your choice (rock/paper/scissors): ").strip().lower()
return user_choice
def get_computer_choice():
"""
Function to generate computer's choice using random module
"""
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
return computer_choice
def determine_winner(user_choice, computer_choice):
"""
Function to determine the winner between user and computer
"""
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'paper' and computer_choice == 'rock') or \
(user_choice == 'scissors' and computer_choice == 'paper'):
return "You win!"
else:
return "Computer wins!"
def main():
print("Welcome to Rock, Paper, Scissors!")
play_again = 'yes'
while play_again == 'yes':
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)
play_again = input("\nDo you want to play again? (yes/no): ").strip().lower()
print("\nThanks for playing!")
if __name__ == "__main__":
main()