-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock_paper_scissors_updated.py
76 lines (59 loc) · 2.34 KB
/
rock_paper_scissors_updated.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
65
66
67
68
69
70
71
72
73
74
75
76
"""
Created on Mon Oct 16 13:43:26 2023
Update: Implement multi-round gameplay.
Track the scores of both the player and the computer.
@author: danielcook
"""
import random
def get_player_choice():
"""Prompt the user to enter a choice and validate it."""
player_choice = input("\nEnter your choice (rock/paper/scissors): ").lower()
while player_choice not in ["rock", "paper", "scissors"]:
player_choice = input('Invalid choice. Please enter rock, paper, or scissors: ').lower()
return player_choice
def get_winner(player_choice, computer_choice):
"""Determine the winner based on choices."""
if player_choice == computer_choice:
return "tie"
elif player_choice == "rock":
return "player" if computer_choice == "scissors" else "computer"
elif player_choice == "paper":
return "player" if computer_choice == "rock" else "computer"
else: # player_choice == "scissors"
return "player" if computer_choice == "paper" else "computer"
def display_round_result(player_choice, computer_choice, winner):
"""Display the results of a round."""
print(f"\nYou chose {player_choice}.")
print(f"Computer chose {computer_choice}.")
if winner == "tie":
print("It's a tie!")
elif winner == "player":
print("You win this round!")
else:
print("Computer wins this round!")
def main():
print("Let's play Rock, Paper, Scissors!")
num_rounds = int(input("How many rounds would you like to play? "))
player_score = 0
computer_score = 0
for _ in range(num_rounds):
player_choice = get_player_choice()
computer_choice = random.choice(['rock', 'paper', 'scissors'])
winner = get_winner(player_choice, computer_choice)
display_round_result(player_choice, computer_choice, winner)
if winner == "player":
player_score += 1
elif winner == "computer":
computer_score += 1
# Final score
print("\n--- Final Scores ---")
print(f"Player: {player_score}")
print(f"Computer: {computer_score}")
if player_score > computer_score:
print("Congratulations! You won the game!")
elif player_score < computer_score:
print("Computer wins the game. Better luck next time!")
else:
print("It's a tie game!")
if __name__ == "__main__":
main()