From 444fe0626922ebfc0fb6edc72d5ec80e9edfba05 Mon Sep 17 00:00:00 2001 From: Olha Yukhnenko Date: Fri, 13 Dec 2024 14:38:55 +0200 Subject: [PATCH] solve_task --- app/main.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..f7cc2f8e 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,39 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + self.hidden = False + # add animal to the alive list when it's created + + Animal.alive.append(self) + + def die(self): + if self.health <= 0: + Animal.alive.remove(self) + + def __repr__(self): + return (f"" + f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}" + ) + + +class Herbivore(Animal): + def hide(self): + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(herbivore): + if isinstance(herbivore, Herbivore) and not herbivore.hidden: + herbivore.health -= 50 + if herbivore.health <= 0: + herbivore.die() + + @staticmethod + def remove_dead_animal(): + Animal.alive = [animal for animal in Animal.alive if animal.health > 0]