Created
December 25, 2017 08:17
-
-
Save shivankgtm/d8f12b197b7b81f1d777a6e11ddb8cb1 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
# 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 name_to_number(name): | |
# delete the following pass statement and 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 | |
else: | |
return 4 | |
# convert name to number using if/elif/else | |
# don't forget to return the result! | |
def number_to_name(number): | |
# delete the following pass statement and fill in your code below | |
if number == 0: | |
player_choice = 'rock' | |
return player_choice | |
elif number == 1: | |
player_choice = 'Spock' | |
return player_choice | |
elif number == 2: | |
player_choice = 'paper' | |
return player_choice | |
elif number == 3: | |
player_choice = 'lizard' | |
return player_choice | |
else: | |
player_choice = 'scissors' | |
return player_choice | |
# convert number to a name using if/elif/else | |
# don't forget to return the result! | |
def rpsls(player_choice): | |
# delete the following pass statement and fill in your code below | |
# print a blank line to separate consecutive games | |
# print out the message for the player's choice | |
##print('player"s" choise =' , player_choice) | |
# convert the player's choice to player_number using the function name_to_number() | |
k = name_to_number(player_choice) | |
print('player_choice = ',player_choice) | |
# compute random guess for comp_number using random.randrange() | |
p = random.randrange(5) | |
# convert comp_number to comp_choice using the function number_to_name() | |
s = number_to_name(p) | |
print('computer"s" = ', s) | |
# print out the message for computer's choice | |
# compute difference of comp_number and player_number modulo five | |
diff = (k-p) | |
print('diff',diff) | |
# use if/elif/else to determine winner, print winner message | |
if diff == 1 or diff == 2 or diff == -3 or diff == -4: | |
print('Player wins') | |
elif diff == 0: | |
print('Ties') | |
else: | |
print('computer wins') | |
# test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED 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
Stone Paper scissors game