即将新换电脑为了防止自己所做过的项目丢失,今天开始,就把一些做过的小东西上传到这里,以后也方便查看。
效果图:
第一次作业比较简单,上代码不多说。
# 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
# helper functions
import random
def name_to_number(guess):
# delete the following pass statement and fill in your code below
pass
# convert name to number using if/elif/else
# don't forget to return the result!
if guess == "rock" :
i=0;
elif guess == "Spock":
i=1;
elif guess == "paper":
i=2;
elif guess == "lizard":
i=3;
elif guess == "scissors":
i=4;
return i
def number_to_name(j):
# delete the following pass statement and fill in your code below
pass
# convert number to a name using if/elif/else
# don't forget to return the result!
if j == 0 :
name = "rock";
elif j== 1:
name = "Spock";
elif j == 2:
name = "paper";
elif j == 3:
name = "lizard";
elif j == 4:
name = "scissors";
return name
def rpsls(guess):
# delete the following pass statement and fill in your code below
pass
# print a blank line to separate consecutive games
# print out the message for the player's choice
# convert the player's choice to player_number using the function name_to_number()
# compute random guess for comp_number using random.randrange()
# convert comp_number to comp_choice using the function number_to_name()
# print out the message for computer's choice
# compute difference of comp_number and player_number modulo five
# use if/elif/else to determine winner, print winner message
print ""
print "Player choose",guess
i = name_to_number(guess)
def random_dice():
j = random.randrange(0,4)
return j
j = random_dice()
name = number_to_name(j)
print "Computer choose", name
m = ( i - j ) % 5
if m == 0:
print "Player and computer tie!"
elif m > 2:
print "Computer wins!"
else:
print "Player 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