-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrock_paper_scissor.py
64 lines (50 loc) · 1.92 KB
/
rock_paper_scissor.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import random
def print_options():
print("\nChoices:")
print("1 - Rock")
print("2 - Paper")
print("3 - Scissors\n")
def get_user_choice():
choice = input("Enter your choice (1, 2, or 3): ")
while choice not in ["1", "2", "3"]:
print("Invalid choice. Please try again.\n")
print_options()
choice = input("Enter your choice (1, 2, or 3): ")
return choice
def get_computer_choice():
return str(random.randint(1, 3))
def print_round_result(user_choice, comp_choice, user_score, comp_score):
choices_mapping = {"1": "Rock", "2": "Paper", "3": "Scissors"}
user_choice_str = choices_mapping[user_choice]
comp_choice_str = choices_mapping[comp_choice]
print(f"You chose {user_choice_str}")
print(f"Computer chose {comp_choice_str}")
if user_choice == comp_choice:
print("It's a tie!")
elif (user_choice == "1" and comp_choice == "3") or \
(user_choice == "2" and comp_choice == "1") or \
(user_choice == "3" and comp_choice == "2"):
print("You win this round!")
user_score += 1
else:
print("Computer wins this round!")
comp_score += 1
print(f"Score: You {user_score}, Computer {comp_score}\n")
def print_final_result(user_score, comp_score):
if user_score > comp_score:
print("Congratulations! You win!")
elif user_score < comp_score:
print("Sorry, computer wins. Better luck next time.")
else:
print("It's a tie!")
print("Rock Paper Scissors\n")
rounds = int(input("Best out of how many rounds? "))
print_options()
user_score = 0
comp_score = 0
for round_num in range(1, rounds + 1):
print(f"\nRound {round_num}:")
user_choice = get_user_choice()
comp_choice = get_computer_choice()
print_round_result(user_choice, comp_choice, user_score, comp_score)
print_final_result(user_score, comp_score)