From 30d4e7a7c9e575077fc43a799bcf4fb8180e06ba Mon Sep 17 00:00:00 2001 From: Olena Lisova Date: Thu, 28 Sep 2023 17:06:09 -0400 Subject: [PATCH] Done py_herbivores_and_carnivors --- app/main.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..3fb50b9a 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,36 @@ -# 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 "{Name:" \ + + f" {self.name}, Health: {self.health}, Hidden: {self.hidden}"\ + + "}" + + +class Herbivore(Animal): + def hide(self) -> None: + if self.hidden: + self.hidden = False + else: + self.hidden = True + + +class Carnivore(Animal): + def bite(self, animal: Herbivore) -> None: + if (animal in Animal.alive + and isinstance(animal, Herbivore) + and not animal.hidden): + animal.health -= 50 + if animal.health <= 0: + Animal.alive.remove(animal)