为什么最后两个else语句没有执行?
问题描述:
最后两个elif语句不起作用;它只是将我重定向到前两者之一。对于else语句,它不会打印点数。为什么这段代码不工作?谢谢!为什么最后两个else语句没有执行?
def match_duel():
print "do you want to swing right or left"
print "and for how many points?"
choice = raw_input("> ")
points = 0
damage = 0
if "right" in choice:
print "you take a swing right. 20 points."
points = points + 20
print "You have %d points" % points
match_duel()
elif "left" in choice:
print "you take a swing left. 50 points."
points = points + 50
print "You have %d points" % points
match_duel()
elif "hard right" in choice:
print "you take a hard swing right for 100 points."
print "cthulhu strikes back with 100 points."
print "you take half the damage."
points = points + 100
damage = damage - 50
print "You have %d points" % points
print "You have %d damage" % damage
match_duel()
elif "hard left" in choice:
print "you take a hard swing left for 200 points."
print "cthulhu strikes back with 50 points, taken by surprise."
print "you take half the damage"
points = points + 200
damage = damage - 25
print "You have %d points" % points
print "You have %d damage" % damage
match_duel()
else:
print "take an extended hit."
points = points + 300
print "You have %d points" % points
答
这里只有一个elif
声明可以执行。满足条件的第一个将被执行 - 其余的被忽略。
“left”will always无论如何都处于“hard left”状态。与“硬右”相同。因此,底部2 elif
声明将永远不会执行。
想一想:如果在选择中硬选择左边的数字比选择数字左边的数值是多少?因此,哪种情况应该先行? – Bakuriu
因此,在测试一个简单的“左”前测试''硬左''*。 – usr2564301
没错。正如你的代码所代表的,你永远无法达到最后两个条款中的任何一个。 – Prune