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 2 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
61 changes: 60 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
# write your code here
from typing import List


class Animal:
"""
Class representing an animal.
"""
alive: List["Animal"] = []

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alive: List["Animal"] = []
alive: List[Animal] = []

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
self.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}}}"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use () then \

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:
self.__class__.alive.remove(self)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.__class__.alive.remove(self)
self.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