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 #1448

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# write your code here
class Animal:
animal = []

Choose a reason for hiding this comment

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

The class attribute animal is defined but not used. It seems like you intended to use alive instead. Consider renaming animal to alive.

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

Choose a reason for hiding this comment

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

The attribute alive is not defined in the Animal class. It should be animal based on the class attribute defined. Consider changing Animal.alive.append(self) to Animal.animal.append(self).

def health_stats(self):
if (self.health <= 0):
self.health = 0
Animal.remove_dead()

@classmethod
def remove_dead(cls):
cls.alive = [animal for animal in cls.alive if animal.health == 0]

Choose a reason for hiding this comment

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

The logic in remove_dead is incorrect. It should retain animals with health greater than 0. Change the condition to animal.health > 0.

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



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


class Carnivore(Animal):
def bite(self, herbivore):
if isinstance(herbivore, Carnivore):
return "Carnivore cannot bite another carnivor"

Choose a reason for hiding this comment

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

The condition isinstance(herbivore, Carnivore) is incorrect. It should check if herbivore is an instance of Herbivore instead.

elif herbivore.hidden == True:

Choose a reason for hiding this comment

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

There's a typo in the return message. It should be 'Carnivore cannot bite another carnivore'.

return "Carnivore cannot bite hidden herbivore"
else:
herbivore.health -= 50
herbivore.health_stats()
Loading