import random
hp = 100
eh = 100
while hp > 0 and eh > 0:
print("Action? (attack, heal, nothing):")
act = input(">")
attack = random.randint(1, 30)
heal = random.randint(1, 15)
if act == "attack" or "Attack":
eh = eh - attack
print(attack)
print("eh = %s" % eh)
elif act == "heal" or "Heal":
hp = hp + heal
print("You have healed %s points" % heal)
print(hp)
为什么当我输入heal时,它也会运行攻击部分?即使我既不攻击也不治疗它仍然运行攻击部分。你知道吗
首先,我假设if和elif部分缩进以适应while循环。你知道吗
它一直发射攻击部分的原因是你的状况:
基本上等于
意思和
所以事实上总是这样。你知道吗
为了让它工作,你应该重复“act==”部分在“Attack too”之前,所以它是
在此条件下:
or后面的部分总是求值为true。你知道吗
你的意思可能是
虽然更好的方法是
你使用
or
是不正确的。就像你有:任何非空字符串的计算结果都是
True
。你知道吗而是使用:
甚至:
相关问题 更多 >
编程相关推荐