From c0c1a3facd57f3ef1d06c145a2ce36de9e65534c Mon Sep 17 00:00:00 2001 From: Iryna Bulakevych Date: Wed, 11 Oct 2023 22:17:19 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..a596307e 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.health = health + self.name = name + 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(animal: Herbivore) -> None: + if not animal.hidden and type(animal) == Herbivore: + animal.health -= 50 + if animal.health <= 0: + Animal.alive.remove(animal) From 442151a0d5477df2861247815a41a77634a806a9 Mon Sep 17 00:00:00 2001 From: Iryna Bulakevych Date: Thu, 12 Oct 2023 17:07:15 +0300 Subject: [PATCH 2/2] Solution fixed --- app/main.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index a596307e..7f935b8e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,4 @@ class Animal: - alive = [] def __init__(self, @@ -13,9 +12,9 @@ def __init__(self, Animal.alive.append(self) def __repr__(self) -> str: - return f"{{Name: {self.name}, " \ - f"Health: {self.health}, " \ - f"Hidden: {self.hidden}}}" + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}") class Herbivore(Animal): @@ -28,7 +27,7 @@ class Carnivore(Animal): @staticmethod def bite(animal: Herbivore) -> None: - if not animal.hidden and type(animal) == Herbivore: + if not animal.hidden and isinstance(animal, Herbivore): animal.health -= 50 if animal.health <= 0: Animal.alive.remove(animal)