一旦达到负值,如何自动将数字更改为零?

时间:2015-11-25 03:35:09

标签: python numbers

while opponentHealth >= 0 or userHealth >= 0:

        userInput = input()

        if userInput == "attack":
            opponentOne = "Larry"
            userDamage = random.randint(0, 100)
            opponentDamage = random.randint(0, 20)
            opponentHealth = opponentHealth - userDamage

            if(userDamage < 25) and (userDamage > 0):
                print("You roundhouse kick {} in the abdomen and he begins to vomit." .format(opponentOne) or "You punch {} in the stomach and he begins to tear up." .format(opponentOne))
                print("You did {} damage to him" .format(userDamage))
                print("He has {} health remaining." .format(opponentHealth))

            elif(userDamage < 100) and (userDamage > 25):
                print("You drive your foot into {}'s groin with as much force as possible. You hear a high-pitched scream emit from his vocal chords." .format(opponentOne) or "{} is intimidated by you and decides to slap himself mindlessly, in hopes that he will lose faster." .format(opponentOne))
                print("You did {} damage to him; a CRITICAL HIT!" .format(userDamage))
                print("He has {} health remaining." .format(opponentHealth))
            elif userDamage == 100:
                print("{} forfeits... Coward." .format(opponentOne))
                print("You did {} damage to him. INSTANT K.O" .format(userDamage))
                print("He has {} health remaining." .format(opponentHealth))
            else:
                print("Swing and a miss. You missed {}." .format(opponentOne) or "You underestimated {}" .format(opponentOne))
                print("You did {} damage to him." .format(userDamage))
                print("He has {} health remaining." .format(opponentHealth))
        else:
            print("Type 'attack' to attack.")
    continue

所以这基本上是我正在尝试的迷你格斗游戏(第一个程序,除了“Hello World”lol)。每当变量opponentHealth进入底片时,我希望它自动变为0。因此,举例来说,我不想说“他还剩下-13健康状况。”,我希望它说“他剩下的0生命值”。有什么建议吗?

提前致谢!

2 个答案:

答案 0 :(得分:3)

每当您分配到opponentHealth时,只需将最大值设为0.例如:

opponentHealth = max(0, opponentHealth - userDamage)

答案 1 :(得分:0)

opponentHealth = opponentHealth - userDamage
if opponentHealth < 0:
    opponentHealth = 0