Skip to content

Instantly share code, notes, and snippets.

@indig0fox
Created March 5, 2018 05:24
Show Gist options
  • Save indig0fox/d6d65d5a26c51d3169a96f1bd2005638 to your computer and use it in GitHub Desktop.
Save indig0fox/d6d65d5a26c51d3169a96f1bd2005638 to your computer and use it in GitHub Desktop.
"""
This is a game of Rock, Paper, Scissors against the computer!
"""
from random import randint
from time import sleep
options = ["R", "P", "S"]
LOSE_MESSAGE = "Sorry, better luck next time!\n"
WIN_MESSAGE = "You won, nice work!\n"
def decide_winner(user_choice, computer_choice):
print "\nYou chose %s.\n" % user_choice
print "Computer selecting..."
sleep(1)
print "The computer chose %s.\n" % computer_choice
user_choice_index = options.index(user_choice)
computer_choice_index = options.index(computer_choice)
if user_choice == computer_choice:
print "You tied!\n"
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 choice.\n"
return
else:
print LOSE_MESSAGE
def play_RPS():
print "Welcome to Rock, Paper, Scissors!"
user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ").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