Created
October 20, 2023 00:08
-
-
Save tejashah88/f15f25b1788971997e8c9edd57b70ae6 to your computer and use it in GitHub Desktop.
Basic rock-paper-scissors game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
CHOICES = ['R', 'P', 'S'] | |
# Ask user for input (rock, paper, or scissors) | |
user_input = input('Rock, paper or scissors (type R, P, or S): ') | |
# Check if user input is valid | |
if user_input not in CHOICES: | |
print('Invalid choice') | |
exit() | |
# Ask computer to generate a random response | |
computer_input = random.choice(CHOICES) | |
# Compare between user input and computer response and print result of the game | |
# Tie condition | |
if user_input == computer_input: | |
print("It's a tie") | |
# User win condition | |
elif user_input == 'R' and computer_input == 'S': | |
print('You win') | |
elif user_input == 'S' and computer_input == 'P': | |
print('You win') | |
elif user_input == 'P' and computer_input == 'R': | |
print('You win') | |
# User lose condition | |
elif user_input == 'S' and computer_input == 'R': | |
print('You lose') | |
elif user_input == 'P' and computer_input == 'S': | |
print('You lose') | |
elif user_input == 'R' and computer_input == 'P': | |
print('You lose') | |
else: | |
print('An error occurred') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment