An Introduction to Interactive Programming in Python
Mini-project description — Rock-paper-scissors-lizard-Spock
Mini-project development process
- Build a helper function
name_to_number(name)
that converts the string name
into a number between 0 and 4 as described above. This function should use a sequence ofif
/elif
/else
clauses. You can use conditions of the form name == 'paper'
, etc. to distinguish the cases. To make debugging your code easier, we suggest including a finalelse
clause that catches cases when name
does not match any of the five correct input strings and prints an appropriate error message. You can test your implementation ofname_to_number()
using this name_to_number testing template. (Also available in the Code Clinic tips thread).
- Next, you should build a second helper function
number_to_name(number)
that converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final else
clause that catches cases when number
is not in the correct range. You can test your implementation of number_to_name()
using this number_to_name testing template.
- Implement the first part of the main function
rpsls(player_choice)
. Print out a blank line (to separate consecutive games) followed by a line with an appropriate message describing the player's choice. Then compute the number player_number
between 0 and 4 corresponding to the player's choice by calling the helper function name_to_number()
usingplayer_choice
.
- Implement the second part of
rpsls()
that generates the computer's guess and prints out an appropriate message for that guess. In particular, compute a random numbercomp_number
between 0 and 4 that corresponds to the computer's guess using the function random.randrange()
. We suggest experimenting with randrange
in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range. Then compute the name comp_name
corresponding to the computer's number using the function number_to_name()
and print an appropriate message with the computer's choice to the console.
- Implement the last part of
rpsls()
that determines and prints out the winner. Specifically, compute the difference between comp_number
and player_number
taken modulo five. Then write an if/elif/else
statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner. If you have trouble deriving the conditions for the clauses of this if/elif/else
statement, we suggest reviewing the "RPSLS" video which describes a simple test for determine the winner of RPSLS.
This will be the only mini-project in the class that is not an interactive game. Since we have not yet learned enough to allow you to play the game interactively, you will simply call yourrpsls
function repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.
The output of running your program should have the following form:
Player chooses rock
Computer chooses scissors
Player wins!
Player chooses Spock
Computer chooses lizard
Computer wins!
Player chooses paper
Computer chooses lizard
Computer wins!
Player chooses lizard
Computer chooses scissors
Computer wins!
Player chooses scissors
Computer chooses Spock
Computer wins!
Note that, for this initial mini-project, we will focus only on testing whether your implementation of rpsls()
works correctly on valid input.
name_to_number(name)
that converts the string name
into a number between 0 and 4 as described above. This function should use a sequence ofif
/elif
/else
clauses. You can use conditions of the form name == 'paper'
, etc. to distinguish the cases. To make debugging your code easier, we suggest including a finalelse
clause that catches cases when name
does not match any of the five correct input strings and prints an appropriate error message. You can test your implementation ofname_to_number()
using this name_to_number testing template. (Also available in the Code Clinic tips thread).number_to_name(number)
that converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final else
clause that catches cases when number
is not in the correct range. You can test your implementation of number_to_name()
using this number_to_name testing template.rpsls(player_choice)
. Print out a blank line (to separate consecutive games) followed by a line with an appropriate message describing the player's choice. Then compute the number player_number
between 0 and 4 corresponding to the player's choice by calling the helper function name_to_number()
usingplayer_choice
.rpsls()
that generates the computer's guess and prints out an appropriate message for that guess. In particular, compute a random numbercomp_number
between 0 and 4 that corresponds to the computer's guess using the function random.randrange()
. We suggest experimenting with randrange
in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range. Then compute the name comp_name
corresponding to the computer's number using the function number_to_name()
and print an appropriate message with the computer's choice to the console.rpsls()
that determines and prints out the winner. Specifically, compute the difference between comp_number
and player_number
taken modulo five. Then write an if/elif/else
statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner. If you have trouble deriving the conditions for the clauses of this if/elif/else
statement, we suggest reviewing the "RPSLS" video which describes a simple test for determine the winner of RPSLS.rpsls
function repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.rpsls()
works correctly on valid input.
SOLUTION
# 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 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
import random
def name_to_number(name):
""" Convert name to number """
if name == "rock":
number = 0
elif name == "Spock":
number = 1
elif name == "paper":
number = 2
elif name == "lizard":
number = 3
elif name == "scissors":
number = 4
return number
def number_to_name(number):
""" Convert number to a name """
if number == 0:
name = "rock"
elif number == 1:
name = "Spock"
elif number == 2:
name = "paper"
elif number == 3:
name = "lizard"
elif number == 4:
name = "scissors"
return name
def rpsls(player_choice):
# 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_number()
player_number = name_to_number(player_choice)
# compute random guess for comp_number using random.randrange()
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
result = (comp_number - player_number) % 5
# determine winner, print winner message
if result == 1 or result == 2:
print "Computer wins!"
elif result == 3 or result == 4:
print "Player wins!"
else:
print "Player and computer tie!"
# test your code - LEAVE THESE CALLS IN YOUR SUBMITTED CODE
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
The code reference (CodeSkulptor )
perde modelleri
ОтветитьУдалитьSms onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
lisans satın al
ОтветитьУдалитьyurtdışı kargo
nft nasıl alınır
en son çıkan perde modelleri
en son çıkan perde modelleri
uc satın al
minecraft premium
özel ambulans