def rpsls(name):
'''
(string) -> string
This function find and print Player choice by
name and computer choise by random number and
who wins according to scope.if player enter wrong
number return error message
'''
# convert name to player_number using name_to_number
player_number = name_to_number(name)
# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(0,5)
# compute difference of player_number and comp_number modulo five
compute_difference = (player_number - comp_number) % 5
# use if/elif/else to determine winner
if compute_difference > 2:
winner = "computer wins"
elif compute_difference == '':
winner = ''
elif compute_difference == 0:
winner = 'player and computer tie!'
else:
winner = "player wins"
# convert comp_number to name using number_to_name
comp_name = number_to_name(comp_number)
# print results
if winner == '':
print "try again"
else:
print ''
print 'Player chooses '+ name
print 'computer chooses '+ comp_name
print winner
我不知道为什么但这段代码不起作用。它说这是问题所在。为了清楚,我想要它。请帮助我。这是一项任务。但它已经结束了,我还没完成它
答案 0 :(得分:1)
在这里你回答整个程序:
Rock-paper-scissors-lizard-Spock模板
import random
这个程序的关键思想是将字符串等同于
“摇滚”,“纸”,“剪刀”,“蜥蜴”,“斯波克”号
如下:
辅助功能
def number_to_name(number):
# fill in your code below
# convert number to a name using if/elif/else
# don't forget to return the result!
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'
def name_to_number(name):
# fill in your code below
# convert name to number using if/elif/else
# don't forget to return the result!
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
def rpsls(name):
# fill in your code below
# convert name to player_number using name_to_number
player_number = name_to_number(name)
# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(5)
# compute difference of player_number and comp_number modulo five
difference = (player_number - comp_number) % 5
# use if/elif/else to determine winner
if difference in [1, 2]:
winmsg = "Player wins!"
elif difference == 0:
winmsg = "Player and computer tie!"
else:
winmsg = "Computer wins!"
# convert comp_number to name using number_to_name
comp_name = number_to_name(comp_number)
# print results
print " "
print "Player chooses " + str(name)
print "Computer chooses " + str(comp_name)
print winmsg
测试你的代码
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")