为什么后面的两个if语句没有执行?

2024-05-12 17:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

最后两个elif语句不起作用;它只是将我重定向到前两个。对于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

Tags: inrightyouhavematchleftpointsprint
1条回答
网友
1楼 · 发布于 2024-05-12 17:30:56

这里只能执行一个elif语句。满足条件的第一个将被执行,其余的将被忽略

不管怎样,“左”总是在“硬左”中。与“右倾”相同。因此,底部的2elif语句将永远不会执行

相关问题 更多 >