Skip to content

Instantly share code, notes, and snippets.

@khayyamsaleem
Created August 8, 2017 19:41
Show Gist options
  • Save khayyamsaleem/85151bef72e731163a42fd5fdd956d95 to your computer and use it in GitHub Desktop.
Save khayyamsaleem/85151bef72e731163a42fd5fdd956d95 to your computer and use it in GitHub Desktop.
#The program should do the following:Prompt the user to select either Rock, Paper, or Scissors. Instruct the computer to randomly select either Rock, Paper, or Scissors. Compare the user's choice and the computer's choice. Determine a winner (the user or the computer). Inform the user who the winner is
from random import randint
from time import sleep
options = ["R", "P", "S"]
LOSS_MESSAGE = "You lost!"
WIN_MESSAGE = "You won!"
def decide_winner(user_choice, computer_choice):
print "You selected: %s" % user_choice
sleep(1)
print "Computer selected: %s" % computer_choice
sleep(1)
user_choice_index=options.index(user_choice)
computer_choice_index=options.index(computer_choice)
if user_choice_index == computer_choice_index:
print "It's a tie!"
elif user_choice_index == 0 and computer_choice_index == 2:
print WIN_MESSAGE
elif user_choice_index==1 and computer_choice_index==0:
print WIN_MESSAGE
elif user_choice_index==2 and computer_choice_index==1:
print WIN_MESSAGE
elif user_choice_index>2:
print "Invalid Option! Try Again!"
else:
print LOSS_MESSAGE
def play_RPS():
print "Rock, Paper, or Scissors?"
user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ")
user_choice = user_choice.upper()
sleep(1)
computer_choice = options [randint(0, len(options) - 1)]
decide_winner(user_choice, computer_choice)
play_RPS()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment