im trying to make a while loop for a health bar but am getting a division error

Viewed 78

Hello so I have a small snippet of code from my larger code that is meant for displaying the health bar of an enemy in a game but for some reason, the loop is continuing even after I think it should stop, is there something I'm missing or doing wrong?

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

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

This part is wrong. Your are comparing alive with False

It should be

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

Sidenote: You dont need to use pass in every single if statement.

Related