From 1fae7b0f7ed7b90efbbd3c06fdf9d36aea4c00e1 Mon Sep 17 00:00:00 2001 From: Yasha Lapin Date: Wed, 18 Dec 2024 16:43:13 +0200 Subject: [PATCH] herbivores --- app/main.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..21519d90 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,40 @@ -# write your code here +class Animal: + alive = [] + + def __init__( + self, name: str, + health: int = 100, + hidden: bool = False + ) -> None: + self.name = name + self.health = health + self.hidden = hidden + Animal.alive.append(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") + + def check_health(self) -> None: + if self.health <= 0: + if self in Animal.alive: + Animal.alive.remove(self) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore): + if not herbivore.hidden: + herbivore.health -= 50 + herbivore.check_health() + else: + print(f"{herbivore.name} is hidden and cannot be bitten.") + else: + print("This is not a herbivore.")