0% found this document useful (0 votes)
99 views2 pages

Miniproject1 Rpsls

This document outlines a rock-paper-scissors-lizard-Spock game. It defines functions to convert between names (rock, paper, etc.) and numbers, randomly generate the computer's choice, calculate who wins based on the difference between numbers, and print out the results. The functions and game are tested with sample calls at the end.

Uploaded by

jcvoscrib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views2 pages

Miniproject1 Rpsls

This document outlines a rock-paper-scissors-lizard-Spock game. It defines functions to convert between names (rock, paper, etc.) and numbers, randomly generate the computer's choice, calculate who wins based on the difference between numbers, and print out the results. The functions and game are tested with sample calls at the end.

Uploaded by

jcvoscrib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Rock-paper-scissors-lizard-Spock template

#
#
#
#
#
#
#
#
#

The key idea of this program is to equate the strings


"rock", "paper", "scissors", "lizard", "Spock" to numbers
as follows:
0
1
2
3
4

rock
Spock
paper
lizard
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
elif name=='scissors':return 4
else :print 'Not a valid name!!'
# 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 : return 'rock'
elif number==1: return 'Spock'
elif number==2:
return 'paper'
elif number==3:
return 'lizard'
elif number==4:
return 'scissors'
else : print 'Not a valid number'
# 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 ''
# print out the message for the player's choice
print 'Player chooses', player_choice
# convert the player's choice to player_number using the function name_to_nu
mber()
player_number=name_to_number(player_choice)
# compute random guess for comp_number using random.randrange()
import random
comp_number=random.randrange(0,5)
# convert comp_number to comp_choice using the function number_to_name()
comp_choice=number_to_name(comp_number)
# print out the message for computer's choice
print 'Computer chooses',comp_choice
# compute difference of comp_number and player_number modulo five
diff=(comp_number-player_number)%5

print diff
# use if/elif/else to determine winner, print winner message
winner=None
if diff==1 or diff ==2:
winner='Computer'
elif diff==3 or diff==4: winner='Player'
elif diff==0:winner='tie'
else: print 'Not a valid difference'
if winner=='tie': print 'It\'s a tie!!'
else :print winner,'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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy