You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
54 lines
1.4 KiB
import random |
|
|
|
def play_round(): |
|
choices = ["rock", "paper", "scissors"] |
|
computer_choice = random.choice(choices) |
|
|
|
while True: |
|
user_choice = input("Enter rock, paper, or scissors: ").lower() |
|
if user_choice in choices: |
|
break |
|
else: |
|
print("Invalid choice. Please enter rock, paper, or scissors.") |
|
|
|
print(f"You chose: {user_choice}") |
|
print(f"Computer chose: {computer_choice}") |
|
|
|
if user_choice == computer_choice: |
|
print("It's a tie!") |
|
return 0 |
|
elif ( |
|
(user_choice == "rock" and computer_choice == "scissors") |
|
or (user_choice == "paper" and computer_choice == "rock") |
|
or (user_choice == "scissors" and computer_choice == "paper") |
|
): |
|
print("You win!") |
|
return 1 |
|
else: |
|
print("You lose!") |
|
return -1 |
|
|
|
def main(): |
|
user_score = 0 |
|
computer_score = 0 |
|
|
|
print("Welcome to Rock, Paper, Scissors!") |
|
|
|
while True: |
|
result = play_round() |
|
if result == 1: |
|
user_score += 1 |
|
elif result == -1: |
|
computer_score += 1 |
|
|
|
print(f"Your score: {user_score}, Computer score: {computer_score}") |
|
|
|
play_again = input("Play again? (yes/no): ").lower() |
|
if play_again != "yes": |
|
break |
|
|
|
print("Thanks for playing!") |
|
print(f"Final score. Your score: {user_score}, Computer score: {computer_score}") |
|
|
|
if __name__ == "__main__": |
|
main()
|
|
|