虽然循环没有结束

时间:2014-06-04 14:03:56

标签: python

while playerhealth and monsterhealth > 0:
        x=raw_input('Attack or Run? >')
        if x=='Attack' or 'attack':
            print 'You attack the %r!' %monster
            if playerstats[2]>=monsterstats[3]:
                print 'You attack!'
                currmonhealth-=(playerstats[0]-monsterstats[2])
                print 'The monster attacks!'
                playerhealth-=(monsterstats[1]-playerstats[1])
                if monsterhealth<=0:
                    return 'The monster died!'
                if playerhealth<=0:
                    return 'You died!'
                print currmonhealth
                print playerhealth
            if monsterstats[2]<monsterstats[3]:
                print 'The monster attacks!'
                playerhealth-=(monsterstats[1]-playerstats[1])
                print 'You attack!'
                print currmonhealth
                print playerhealth
                currmonhealth-=(playerstats[0]-monsterstats[2])
                if monsterhealth<=0:
                    return 'The monster died!'
                if playerhealth<=0:
                    return 'You died!'
        elif x=='Run' or 'run':
            c=random.randint(0,1)
            if c==0:
                return 'You made it!'
            if c==1:
                print 'You didn\'t make it!'
                print 'The monster attacks!'
                playerhealth-=(monsterstats[1]-playerstats[1])
                if playerhealth<=0:
                    return 'You died!'
        else:
            print 'You can\'t do that!'

*对不起这一切。当我尝试运行它时会打印出怪物的生命值和玩家的生命值,但是让它在零结束时不会结束循环。任何人都可以解释为什么循环没有结束的问题? *

2 个答案:

答案 0 :(得分:2)

你应该使用

playerhealth > 0 and monsterhealth > 0:

您的代码不起作用,因为playerhealth < 0monsterhealth > 0您的条件评估为True

答案 1 :(得分:0)

你的问题在于这一行:

while playerhealth and monsterhealth > 0:

它正在做的是检查playerhealth变量是否具有非零值以及monsterhealth是否大于0.这是truth value检查。

您错过了playerhealth的值检查。这将检查两个值是否都高于0。

while playerhealth > 0 and monsterhealth > 0: