你好,所以我有一個來自較大代碼的一小段代碼,用于顯示游戲中敵人的生命值,但由于某種原因,即使我認為它應該停止,循環仍在繼續,我有什么嗎失蹤或做錯了?import pygame, sysimport timeimport randomimport osimport sqlite3import os.pathdamage_done = 1random_monster_health = random.randint(7,10)alive = Trueprint (random_monster_health)while alive == True: mob_health = random_monster_health - damage_done print ("mob health is {}" .format(mob_health)) if mob_health != 0: percent_damage_done = mob_health / int(random_monster_health) how_much_health_bar = 1180 * (percent_damage_done) else: pass random_monster_health = mob_health print(random_monster_health) if mob_health != 0: print("the monster is still alive") pass else: alive == False print ("the monster is dead") passprint("the loop has ended")
1 回答

動漫人物
TA貢獻1815條經驗 獲得超10個贊
else:
alive == False
print ("the monster is dead")
pass
這部分是錯誤的。你正在alive比較False
它應該是
else:
alive = False
print ("the monster is dead")
旁注:您不需要在每個 if 語句中使用 pass。
添加回答
舉報
0/150
提交
取消