-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Solution #1448
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
# write your code here | ||
class Animal: | ||
animal = [] | ||
|
||
def __init__(self, name, hidden=False, health=100): | ||
self.health = health | ||
self.name = name | ||
self.hidden = hidden | ||
Animal.alive.append(self) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute |
||
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] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in |
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
elif herbivore.hidden == True: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
There was a problem hiding this comment.
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 usealive
instead. Consider renaminganimal
toalive
.