From 3707cdd5d1d41d966094c1b2f4411c346ee85b9d Mon Sep 17 00:00:00 2001 From: Aeros72 Date: Sat, 14 Oct 2023 17:42:00 +0300 Subject: [PATCH] Implemented py-herbivores-and-carnivores --- app/main.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..46c37f96 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,34 @@ -# 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}}}" + ) + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(herb: Herbivore) -> None: + if isinstance(herb, Herbivore) and herb.hidden is False: + herb.health -= 50 + if herb.health <= 0: + Animal.alive.remove(herb)