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

task completed #581

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 58 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# write your code here
class Animal:
"""
Class representing an animal.
"""

alive = []

def __init__(self, name: str, health: int = 100) -> None:
"""
Initialize an animal with a name and health.
"""
self.name = name
self.health = health
self.hidden = False
Animal.alive.append(self)

def __repr__(self) -> str:
"""
Represent the animal as a string.
"""
return (f"{{Name: {self.name}, "
f"Health: {self.health}, "
f"Hidden: {self.hidden}}}")

def check_health(self) -> None:
"""
Check the health of the animal.
If the health is 0 or less, remove the animal from the alive list.
"""
if self.health <= 0:
Animal.alive.remove(self)


class Herbivore(Animal):
"""
Class representing a herbivore, which is a type of animal.
"""

def hide(self) -> None:
"""
Change the hidden status of the herbivore.
"""
self.hidden = not self.hidden


class Carnivore(Animal):
"""
Class representing a carnivore, which is a type of animal.
"""

def bite(self, other: "Herbivore") -> None:
"""
Bite another herbivore, decreasing its health by 50.
If the other animal is not a herbivore or is hidden, do nothing.
"""
if isinstance(other, Herbivore) and not other.hidden:
other.health -= 50
other.check_health()
Loading