print("You see an orc standing in your path. There is no way out but
through it.")
print("0. Escape\n1. Fight\n2. Defend\n3. Heal")
Action = int(input("You grip your sword tightly and think about what to do
next."))
while Action (!= 0) or (OrcHealth <=0):
if Action == 1:
HeroAttack=random.randint(1,5)
OrcHealth = OrcHealth - HeroAttack
print("You see an opening, this is your chance! You swing your blade
and do",HeroAttack,"to the orc.\n This brings the beast down to",
OrcHealth)
Action = int(input("You grip your sword tightly and think about what
to do next."))
在第6行,我开始我的while循环。它半工作,但当OrcHealth达到0或更低时,while循环不会像我想要的那样终止。有谁知道我做错了什么?
答案 0 :(得分:0)
你应该给How to debug small programs (#2) 阅读。
您可以通过将or
切换为and
并更改orc-health的“仍然有效检查”来修复您的代码。
我做了一些调整,以使其成为一个mvce:
import random
print("You see an orc standing in your path. There is no way out but through it.")
print("0. Escape\n1. Fight\n2. Defend\n3. Heal")
Action = int(input("You grip your sword tightly and think about what to do next."))
OrcHealth = 50
while (Action != 0) and (OrcHealth > 0):
if Action == 1:
HeroAttack=random.randint(1,5)
OrcHealth = OrcHealth - HeroAttack
print("You see an opening, this is your chance! You swing your blade and do",
HeroAttack,"to the orc.\n This brings the beast down to", max(0,OrcHealth))
Action = int(input("You grip your sword tightly and think about what to do next."))
print("The orc is dead - you win." if OrcHealth <= 0 else
"You stumble, the orc chops your head off. You die. Told you: no way out!")
另外值得一读:de Morgans laws - 或者如何否定AND / OR'逻辑运算。