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

'Solution' #1579

Open
wants to merge 1 commit 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
30 changes: 29 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# write your code here
class Animal:
alive = []

Choose a reason for hiding this comment

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

The alive list is a class variable, meaning it is shared across all instances of Animal. If the intention is to track all living animals, this is correct. However, if each instance should have its own list, consider making it an instance variable instead.

def __init__(self, name: str, health: int = 100, hidden: bool = False):
self.health = health
self.name = name
self.hidden = hidden
Animal.alive.append(self)

def __str__(self):
return (f"{{Name: {self.name}, Health: {self.health}, "
f"Hidden: {self.hidden}}}")

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


class Carnivore(Animal):
def bite(self, herbivore):
if isinstance(herbivore, Herbivore) and not herbivore.hidden:
herbivore.health -= 50

Choose a reason for hiding this comment

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

The condition not herbivore.hidden is correctly checking if the herbivore is not hidden before allowing the carnivore to bite. Ensure that this logic aligns with the intended game mechanics.

if herbivore.health <= 0:
Animal.alive.remove(herbivore)

Choose a reason for hiding this comment

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

Removing the herbivore from Animal.alive when its health is less than or equal to zero is correct. Ensure that this logic is consistent with the intended behavior for when an animal 'dies'.


class Herbivore(Animal):
def hide(self):
self.hidden = not self.hidden
Loading