python - 我试图为健康栏创建一个 while 循环,但出现除法错误

标签 python loops while-loop pygame

您好,我的较大代码中有一小段代码,用于显示游戏中敌人的生命值,但由于某种原因,即使我认为它应该停止,循环仍在继续,有什么吗?我错过了还是做错了?

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")

最佳答案

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

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

进行比较

应该是

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

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

关于python - 我试图为健康栏创建一个 while 循环,但出现除法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64247225/

相关文章:

python - 如何使用 DRY 配置部署多个 Django 站点?

python - 在DateTime对象中查找日期的索引python

Python int/str 检查

c - 具有不同迭代的循环

php - 使用 PHP SQL 在数据库中查找特定值

java - 查找所有的 xpath 或 cssSelector 位置

php - 查询外部 while 循环,使用内部循环,不起作用

python - 在 apache2 上使用 wsgi 部署 Flask 应用程序时出错

python - 在 Python 命令行参数中拆分长参数

python - 如何循环检查列表中的所有值是否大于另一个列表中的值?