Rock, Paper, Scissors
The code is a simple implementation of the classic "Rock, Paper, Scissors" game where a player competes against the computer, and the first to win two rounds is declared the winner.
The code is a simple implementation of the classic "Rock, Paper, Scissors" game where a player competes against the computer, and the first to win two rounds is declared the winner.
This script allows the user to play a "Rock, Paper, Scissors" game against the computer. The game continues until either the player or the computer wins two rounds. The player is prompted to choose between "rock," "paper," or "scissors," while the computer randomly selects one of these options. The script compares the choices to determine the round's winner and updates the score accordingly. The game announces the final winner once either the player or the computer reaches two wins.
Check out the CodeLogic Correction:
while
loop to announce the overall winner only after the game concludes.print(f"Final Score - Player: {player_wins}, Computer: {computer_wins}")
if player_wins > computer_wins:
print("Congratulations! You won the game.")
else:
print("Computer won the game!")
Input Validation:
while player_choice not in choices:
player_choice = input("Invalid choice. Please choose rock, paper, or scissors: ").lower()
Refactoring for Reusability:
def get_winner(player_choice, computer_choice):
if (player_choice == "rock" and computer_choice == "scissors") or \
(player_choice == "scissors" and computer_choice == "paper") or \
(player_choice == "paper" and computer_choice == "rock"):
return "Player"
elif player_choice == computer_choice:
return "Tie"
else:
return "Computer"
Enhancing User Experience:
if input("Do you want to play another round? (yes/no): ").lower() != "yes":
break
Modularizing the Game Loop:
def play_game():
player_wins = 0
computer_wins = 0
print("Let's play rock, paper, or scissors")
while player_wins < 2 and computer_wins < 2:
player_choice = input("\nChoose rock, paper, or scissors: ").lower()
choices = ["rock", "paper", "scissors"]
if player_choice not in choices:
print("Invalid choice, try again.")
continue
computer_choice = random.choice(choices)
print(f"Computer chose: {computer_choice}")
winner = get_winner(player_choice, computer_choice)
if winner == "Player":
print("You won this round!")
player_wins += 1
elif winner == "Computer":
print("Computer won this round!")
computer_wins += 1
else:
print("It's a tie.")
print(f"Current Score - Player: {player_wins}, Computer: {computer_wins}")
return player_wins, computer_wins
player_wins, computer_wins = play_game()
print(f"Final Score - Player: {player_wins}, Computer: {computer_wins}")
if player_wins > computer_wins:
print("Congratulations! You won the game.")
else:
print("Computer won the game!")
Expanding the Game:
The code successfully implements the basic mechanics of "Rock, Paper, Scissors." By adding input validation, better structuring with functions, and improving the game loop, you can create a more robust and user-friendly game that’s easier to extend and maintain.