lundi 18 avril 2016

Why is 'not' not working when used with a dictionary?

class Enemy(object):
    def __init__(self, name, hp, damage):
       self.name = name
       self.hp = hp
       self.damage = damage

    def is_alive(self):
       return self.hp > 0

enemies = {}

enemies['dog'] = Enemy("Dog", 20, 5)

if enemies['dog'].is_alive:
   print("Woof, Woof!")

enemies['dog'].hp = 0

print(enemies['dog'].hp)

if not enemies['dog'].is_alive:
   print("The dog is dead")

As shown above, I have created a class that has a function which checks the amount of one of the variables. When I run it, it works, however, when is use 'not' to get it to print when it is 0, it does not print anything, even though I changed the variable to 0. I checked it by printing the HP out, after changing it and it did change.

Is it possible whether anyone can tell me why it is not printing? I have tried searching for an answer but am unable to find anything. Please help!

Aucun commentaire:

Enregistrer un commentaire