import random
def get_computer_choice():
choices = ['rock', 'paper', 'scissors']
return random.choice(choices)
def get_user_choice():
while True:
choice = input("Choose rock, paper, or scissors: ").lower()
if choice in ['rock', 'paper', 'scissors']:
return choice
else:
print("Invalid choice. Try again.")
def determine_winner(user, computer):
if user == computer:
return "It's a tie!"
elif (user == 'rock' and computer == 'scissors') or \
(user == 'scissors' and computer == 'paper') or \
(user == 'paper' and computer == 'rock'):
return "You win!"
else:
return "Computer wins!"
def play_game():
player_score = 0
computer_score = 0
while True:
computer_choice = get_computer_choice()
user_choice = get_user_choice()
print(f"Computer chose: {computer_choice}")
result = determine_winner(user_choice, computer_choice)
print(result)
if result == "You win!":
player_score += 1
elif result == "Computer wins!":
computer_score += 1
print(f"Score: You {player_score} - Computer {computer_score}")
if input("Play again? (yes/no): ").lower() != 'yes':
break
if __name__ == "__main__":
play_game()