Skip to content

Instantly share code, notes, and snippets.

@tejashah88
Created October 20, 2023 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejashah88/f15f25b1788971997e8c9edd57b70ae6 to your computer and use it in GitHub Desktop.
Save tejashah88/f15f25b1788971997e8c9edd57b70ae6 to your computer and use it in GitHub Desktop.
Basic rock-paper-scissors game
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