I was doing this Pokemon project on Codecademy, and it asked me to do something that I was completely unable to think of, as I don't have much practice on OOPS and python.
How can I set is_knocked_out = True, without calling any method on my instance? I think that my code should know automatically when the health of pokemon has become zero and automatically change its attribute is_knocked_out to True.
I searched online but didn’t find any definitive solution maybe it has something to do with decorators.
Can someone please explain me how to do this, because I think I might have hit a wall here.
I have written the following code so far:
class Pokemon:
def __init__(self,name,level,ptype,is_knocked_out= False):
self.name = name
self.level = level
self.type = ptype
self.max_health = level
self.curr_health = max_health
self.is_knocked_out = is_knocked_out
def lose_health(self,loss_amount):
self.curr_health -= loss_amount
print(f"{self.name} has now health of {self.curr_health}")
def regain_health(self,gain_amount):
self.curr_health += gain_amount
print(f"{self.name} has now health of {self.curr_health}")
#@property
def knock_out(self):
if self.curr_health <=0:
self.is_knocked_out = True
print(f"{self.name} has been knocked out")
Trueto that attribute:p = Pokemon( ... ); p.is_knocked_out = True