From 78c94fe9f599f29300e32f4d662728f69945ff7c Mon Sep 17 00:00:00 2001 From: zaeba Date: Wed, 27 Nov 2024 14:43:25 +0200 Subject: [PATCH] .!. --- app/main.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..411f02e7 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,35 @@ -# write your code here +class Animal: + alive: list["Animal"] = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + self.hidden = False + Animal.alive.append(self) + + def die(self) -> None: + Animal.alive.remove(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): + def bite(self, target: "Animal") -> None: + if not isinstance(target, Herbivore): + return + if target.hidden: + return + + target.health -= 50 + if target.health <= 0: + target.die()