Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IrynaBrenko committed Nov 28, 2024
1 parent faa256e commit 20b6bcf
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# write your code here
from __future__ import annotations


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)

@property
def hidden(self) -> bool:
return self._hidden

def die(self) -> None:
if self in Animal.alive:
Animal.alive.remove(self)

def __repr__(self) -> str:
return (f""
f"{{Name: {self.name}, "
f"Health: {self.health}, "
f"Hidden: {self.hidden}}}")


class Herbivore(Animal):

def hide(self) -> None:
self._hidden = not self._hidden

@property
def hidden(self) -> bool:
return self._hidden


class Carnivore(Animal):

@staticmethod
def bite(prey: int) -> None:
if isinstance(prey, Herbivore) and not prey.hidden:
prey.health -= 50
if prey.health <= 0:
prey.die()

0 comments on commit 20b6bcf

Please sign in to comment.