如何在达到负值后自动将数字更改为零?
How to automatically change a number to zero once it reaches a negative value?
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" 哈哈)。每当变量 opponentHealth 变成负数时,我希望它自动变成 0。例如,与其说 "He has -13 health remaining.",不如说 "He has 0 health remaining." 有什么建议吗?
提前致谢!
opponentHealth = opponentHealth - userDamage
if opponentHealth < 0:
opponentHealth = 0
无论什么时候给opponentHealth
赋值,取最大值0即可。例如:
opponentHealth = max(0, opponentHealth - userDamage)
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" 哈哈)。每当变量 opponentHealth 变成负数时,我希望它自动变成 0。例如,与其说 "He has -13 health remaining.",不如说 "He has 0 health remaining." 有什么建议吗?
提前致谢!
opponentHealth = opponentHealth - userDamage
if opponentHealth < 0:
opponentHealth = 0
无论什么时候给opponentHealth
赋值,取最大值0即可。例如:
opponentHealth = max(0, opponentHealth - userDamage)