Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
yelizaveta-zh committed Dec 10, 2024
1 parent faa256e commit 5ce6475
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# write your code here
from typing import Any


class Animal:
alive = []

def __init__(self, *args: Any,
name: str, health: int, hidden: bool) -> None:
self.name = name
self.health = 100
self.hidden = False
Animal.alive.append(self)

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

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

def __str__(self) -> str:
return str([repr(animal) for animal in Animal.alive])


class Herbivore(Animal):
def hide(self) -> None:
self.hidden = not self.hidden


class Carnivore(Animal):
def bite(self, prey: Animal) -> None:
if (isinstance(prey, Herbivore)
and not prey.hidden
and prey in Animal.alive):
prey.health -= 50
if prey.health <= 0:
prey.die()

0 comments on commit 5ce6475

Please sign in to comment.