Created
August 8, 2017 19:45
-
-
Save khayyamsaleem/4b7b2b90fc07fd189e7207d07324c9f6 to your computer and use it in GitHub Desktop.
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
#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