如果数字为 <= 0,我该如何停止?



我试图停止我的程序时,战士是牧师都与0或<0或者吸血鬼变成0如果不是0,他们将无法进入下一轮,并以这种方式结束战斗,并建立一个赢家。

while (warrior[0] and priest[0]) > 0 or vampire[0] > 0: #Loop until all of the group die or the enemy dies
first_turn() #Execute the first turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
second_turn() #Execute the second turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
third_turn() #Execute the third turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
initiative_phase()
else:
break

我已经尝试了上面的方法,但我不明白为什么它不停止。

将while条件改为:

while (warrior[0] > 0 or priest[0] > 0) and vampire[0] > 0:

我们添加圆括号是因为逻辑'and'的优先级高于逻辑'or'。如果战士[0]>0为真,则根本不考虑吸血鬼[0]>0是否为真。

似乎你需要括号。

while ((warrior[0] > 0 or priest[0] > 0) and vampire[0] > 0): #Loop until all of the group die or the enemy dies
execute_turn() #Execute the turn
else:  #someone died
break

最新更新