From dc15052e81c5791664856637e9874ba80bbae44a Mon Sep 17 00:00:00 2001 From: Oleksandr Merlenko Date: Wed, 2 Oct 2024 19:48:31 +0200 Subject: [PATCH 1/2] Solution --- app/main.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..c9e54ae8 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,27 @@ -# 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 + Animal.alive.append(self) + + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"Health: {self.health}, Hidden: {self.hidden}}}") + + +class Herbivore(Animal): + def hide(self) -> None: + self.hidden = not self.hidden + + +class Carnivore(Animal): + @staticmethod + def bite(herbivore: Herbivore) -> None: + if herbivore.hidden is False and isinstance(herbivore, Herbivore): + herbivore.health -= 50 + + if herbivore.health <= 0: + Animal.alive.remove(herbivore) From 6da2d6e7be64f90239b1f11a7e8a560b2ef51b8e Mon Sep 17 00:00:00 2001 From: Oleksandr Merlenko Date: Wed, 2 Oct 2024 20:16:18 +0200 Subject: [PATCH 2/2] Update app/main.py Co-authored-by: Vasyl Smutok --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index c9e54ae8..a847507d 100644 --- a/app/main.py +++ b/app/main.py @@ -20,7 +20,7 @@ def hide(self) -> None: class Carnivore(Animal): @staticmethod def bite(herbivore: Herbivore) -> None: - if herbivore.hidden is False and isinstance(herbivore, Herbivore): + if isinstance(herbivore, Herbivore) and herbivore.hidden is False: herbivore.health -= 50 if herbivore.health <= 0: