-
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 #679
Solution #679
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,38 @@ | ||
# write your code here | ||
from typing import Union | ||
|
||
|
||
class Animal: | ||
alive = [] | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
health: int = 100, | ||
hidden: bool = False) -> None: | ||
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. Place |
||
self.name = name | ||
self.health = health | ||
self.hidden = hidden | ||
self.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. Refer to 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. not fixed 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. Append the current instance to the class attribute 'alive' |
||
|
||
@classmethod | ||
def remove_dead_animal(cls) -> None: | ||
cls.alive = [animal for animal in cls.alive if animal.health > 0] | ||
|
||
def __repr__(self) -> str: | ||
return (f"{{Name: {self.name}, " | ||
f"Health: {self.health}, " | ||
f"Hidden: {self.hidden}}}") | ||
|
||
|
||
class Carnivore(Animal): | ||
|
||
def bite(self, target: Union) -> None: | ||
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. Change your type annotation for the target to indicate that you expect a |
||
target.health -= 50 \ | ||
if (isinstance(target, Herbivore) and not target.hidden) else 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. Write this code in multiple lines to avoid line-breaking with |
||
Animal.remove_dead_animal() | ||
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. It would be inefficient to make a perform list comprehension and create a new list each time after the animal is deleted from the |
||
|
||
|
||
class Herbivore(Animal): | ||
|
||
def hide(self) -> None: | ||
self.hidden = not self.hidden |
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.
Do not forget to remove unnecessary imports.