Skip to content

Instantly share code, notes, and snippets.

@ranlix
Created October 22, 2013 02:17
Show Gist options
  • Save ranlix/7094230 to your computer and use it in GitHub Desktop.
Save ranlix/7094230 to your computer and use it in GitHub Desktop.
# Rock-paper-scissors-lizard-Spock template
import random
# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
# helper functions
def number_to_name(number):
# fill in your code below
if number == 0:
return "rock"
elif number == 1:
return "spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
else:
return "scissors"
# convert number to a name using if/elif/else
# don't forget to return the result!
def name_to_number(name):
# fill in your code below
if name == "rock":
return 0
elif name == "spock":
return 1
elif name == "paper":
return 2
elif name == "lizard":
return 3
elif name == "scissors":
return 4
# convert name to number using if/elif/else
# don't forget to return the result!
def rpsls(name):
# fill in your code below
# convert name to player_number using name_to_number
player_guess = name_to_number(name)
# compute random guess for comp_number using random.randrange()
computer_guess = random.randrange(0,4)
# compute difference of player_number and comp_number modulo five
print "Plyer chooses ",name
print "Computer chooses ",number_to_name(computer_guess)
# use if/elif/else to determine winner
if player_guess == computer_guess:
# convert comp_number to name using number_to_name
print "Player and computer tie!"
# print results
elif player_guess == 0:
if computer_guess == 1:
print "Computer wins!"
elif computer_guess == 2:
print "Computer wins!"
elif computer_guess == 3:
print "Player wins!"
else:
print "Player wins!"
elif player_guess == 1:
if computer_guess == 0:
print "Player wins!"
elif computer_guess == 2:
print "Computer wins!"
elif computer_guess == 3:
print "Computer wins!"
else:
print "Player wins!"
elif player_guess == 2:
if computer_guess == 0:
print "Player wins!"
elif computer_guess == 1:
print "Player wins!"
elif computer_guess == 3:
print "Computer wins!"
else:
print "Computer wins!"
elif player_guess == 3:
if computer_guess == 0:
print "Computer wins!"
elif computer_guess == 1:
print "Player wins!"
elif computer_guess == 2:
print "Player wins!"
else:
print "Computer wins!"
else:
if computer_guess == 0:
print "Computer wins!"
elif computer_guess == 1:
print "Computer wins!"
elif computer_guess == 2:
print "Player wins!"
else:
print "Player wins!"
print ""
# test your code
rpsls("rock")
rpsls("spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
# always remember to check your completed program against the grading rubric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment