Add py/random/RockPaperScissors.py
This commit is contained in:
parent
036bce9806
commit
b4a88e49d7
1 changed files with 46 additions and 0 deletions
46
py/random/RockPaperScissors.py
Normal file
46
py/random/RockPaperScissors.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
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!")
|
Loading…
Add table
Reference in a new issue