0

ok so here i got an error help please

So im in python's project called Shooting Game and my only error is that i cant undestand why my code instead of subtract a number of life and save it,just subtract it and then its like it erase the result,it would be easier to undestand if code it class Enemigo: nombre="" vidas=0 def __init__(self, nombre,vidas): self.nombre = nombre self.vidas = vidas def hit(self): self.vidas=self.vidas-1 if self.vidas<=0: print(self.nombre+" killed") else: print(self.nombre+" has "+ str(self.vidas)+" lives") class Monstruo(Enemigo): def __init__(self): super().__init__('Monster', 3) class Alien(Enemigo): def __init__(self): super().__init__('Alien', 5) while True: x = input() if x=='exit': break elif x=="gun": Monstruo().hit() elif x == 'laser': Alien().hit()

20th Feb 2021, 9:24 PM
Yami Francø
Yami Francø - avatar
2 odpowiedzi
+ 2
because every time you loop in that while loop, a new class instance is created. With all new lives too. ..... snippet ..... # create instances outside loop monster = Monsturo() alien = Alien() while True: x = input() if x=='exit': break elif x=="gun": monster.hit() # change inside the loop elif x == 'laser': alien.hit() # change inside the loop
20th Feb 2021, 9:32 PM
Slick
Slick - avatar
+ 2
Thank you very much,it appears an instance of the class but i tought it was optional
20th Feb 2021, 9:37 PM
Yami Francø
Yami Francø - avatar