From 3264f6390c441849543856ddcaff1b5f955099e9 Mon Sep 17 00:00:00 2001 From: JulianskyMe Date: Sun, 22 Dec 2024 23:00:42 +0200 Subject: [PATCH 1/4] Solution --- app/main.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..0dcb6b0e 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,37 @@ -# write your code here +class Animal: + animal = [] + + def __init__(self, name, hidden=False, health=100): + self.health = health + self.name = name + self.hidden = hidden + Animal.alive.append(self) + + def health_stats(self): + if (self.health <= 0): + self.health = 0 + Animal.remove_dead() + + @classmethod + def remove_dead(cls): + cls.alive = [animal for animal in cls.alive if animal.health == 0] + + def __repr__(self): + return f"{{Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}}}" + + + +class Herbivore(Animal): + def hide(self): + self.hidden = not self.hidden + + +class Carnivore(Animal): + def bite(self, herbivore): + if isinstance(herbivore, Carnivore): + return "Carnivore cannot bite another carnivor" + elif herbivore.hidden == True: + return "Carnivore cannot bite hidden herbivore" + else: + herbivore.health -= 50 + herbivore.health_stats() \ No newline at end of file From 585d2ba0992ef9989d369332a6b322b5170f732d Mon Sep 17 00:00:00 2001 From: JulianskyMe Date: Sun, 22 Dec 2024 23:07:48 +0200 Subject: [PATCH 2/4] upd --- app/main.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 0dcb6b0e..af7e97a2 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ class Animal: - animal = [] + alive = [] def __init__(self, name, hidden=False, health=100): self.health = health @@ -14,7 +14,7 @@ def health_stats(self): @classmethod def remove_dead(cls): - cls.alive = [animal for animal in cls.alive if animal.health == 0] + cls.alive = [animal for animal in cls.alive if animal.health > 0] def __repr__(self): return f"{{Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}}}" @@ -30,8 +30,7 @@ class Carnivore(Animal): def bite(self, herbivore): if isinstance(herbivore, Carnivore): return "Carnivore cannot bite another carnivor" - elif herbivore.hidden == True: + elif herbivore.hidden: return "Carnivore cannot bite hidden herbivore" - else: - herbivore.health -= 50 - herbivore.health_stats() \ No newline at end of file + herbivore.health -= 50 + herbivore.health_stats() \ No newline at end of file From de65762f99d6e4689aa5c53331d6b7bf82048577 Mon Sep 17 00:00:00 2001 From: JulianskyMe Date: Sun, 22 Dec 2024 23:08:55 +0200 Subject: [PATCH 3/4] upd 1.1 --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index af7e97a2..e13bc76a 100644 --- a/app/main.py +++ b/app/main.py @@ -28,7 +28,7 @@ def hide(self): class Carnivore(Animal): def bite(self, herbivore): - if isinstance(herbivore, Carnivore): + if isinstance(herbivore, Herbivore): return "Carnivore cannot bite another carnivor" elif herbivore.hidden: return "Carnivore cannot bite hidden herbivore" From a390ee3c8abc0041d6c04819be244156a6d55d7a Mon Sep 17 00:00:00 2001 From: JulianskyMe Date: Sun, 22 Dec 2024 23:13:11 +0200 Subject: [PATCH 4/4] upd 2 --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index e13bc76a..af7e97a2 100644 --- a/app/main.py +++ b/app/main.py @@ -28,7 +28,7 @@ def hide(self): class Carnivore(Animal): def bite(self, herbivore): - if isinstance(herbivore, Herbivore): + if isinstance(herbivore, Carnivore): return "Carnivore cannot bite another carnivor" elif herbivore.hidden: return "Carnivore cannot bite hidden herbivore"