cshw/py/random/RockPaperScissors.py

46 lines
1.2 KiB
Python

import random
# Mapping short inputs to full choices
short_to_full = {
"r": "rock",
"p": "paper",
"s": "scissors"
}
# Available choices
choices = ["rock", "paper", "scissors"]
# Main game loop
while True:
# Computer randomly picks a choice
computer_choice = random.choice(choices)
# Ask user for their choice
user_input = input("Choose rock (r), paper (p), or scissors (s), or type 'quit' to stop: ").lower()
# Exit condition
if user_input == "quit":
print("Thanks for playing!")
break
# Convert short form to full word
user_choice = short_to_full.get(user_input, user_input)
# Check for valid input
if user_choice not in choices:
print("Invalid choice! Please try again.")
continue
print(f"You chose {user_choice}, computer chose {computer_choice}.")
# Determine the result
if user_choice == computer_choice:
print("It's a tie!")
elif (
(user_choice == "rock" and computer_choice == "scissors") or
(user_choice == "scissors" and computer_choice == "paper") or
(user_choice == "paper" and computer_choice == "rock")
):
print("You win!")
else:
print("You lose!")