Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #1398

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Develop #1398

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Create a `Сarnivore` class. This class should inherit from Animal.
Carnivore has a `bite` method, which takes a
herbivore object and decreases the object's health by 50. The method
does not work if it is another сarnivore, or the herbivore is currently hiding.

```python
lion = Carnivore("Lion King")
rabbit = Herbivore("Susan")
Expand Down
52 changes: 51 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# write your code here
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)

def take_damage(self, damage: int) -> None:
self.health -= damage
if self.health < 0:
self.health = 0
print(f"{self.name} took {damage} damage. Health: {self.health}")

if self.health == 0:
self.die()

def die(self) -> None:
if self in Animal.alive:
Animal.alive.remove(self)
print(f"{self.name} is dead.")

def is_alive(self) -> bool:
return self.health > 0

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, herbivore: Herbivore) -> None:
if isinstance(herbivore, Herbivore):
if herbivore.hidden:
print(f"{self.name} cannot bite {herbivore.name} "
f"because they are hidden.")
else:
herbivore.take_damage(50)
print(f"{self.name} bit {herbivore.name}. "
f"{herbivore.name} s health: {herbivore.health}"
)
else:
print(f"{self.name} cannot bite another carnivore.")
Loading