我试着做一个健康条的while循环,但是我得到了一个除法错误

2024-04-30 07:01:36 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我有一小段较大代码中的代码,用于在游戏中显示敌人的健康栏,但出于某种原因,循环在我认为应该停止后仍在继续,我是否遗漏了什么或做错了什么

import pygame, sys
import time
import random
import os
import sqlite3
import os.path

damage_done = 1
random_monster_health = random.randint(7,10)
alive = True
print (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")
        pass

print("the loop has ended")


Tags: the代码importtrueisosrandompass
1条回答
网友
1楼 · 发布于 2024-04-30 07:01:36
else:
    alive == False
    print ("the monster is dead")
    pass

这部分是错误的。您正在比较aliveFalse

应该是

else:
    alive = False
    print ("the monster is dead")

旁注:您不需要在每个if语句中都使用pass

相关问题 更多 >