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

Closed
wants to merge 5 commits into from
Closed
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
39 changes: 38 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# write your code here
from typing import Union

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.



class Animal:
alive = []

def __init__(
self,
name: str,
health: int = 100,
hidden: bool = False) -> None:

Choose a reason for hiding this comment

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

Place ) -> None: on a new line to format your code properly.

self.name = name
self.health = health
self.hidden = hidden
self.alive.append(self)

Choose a reason for hiding this comment

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

Refer to Animal.alive instead of self.alive, because alive is a class attribute.

Choose a reason for hiding this comment

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

not fixed

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

Choose a reason for hiding this comment

The 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 Herbivore instance.

target.health -= 50 \
if (isinstance(target, Herbivore) and not target.hidden) else 0

Choose a reason for hiding this comment

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

Write this code in multiple lines to avoid line-breaking with \. Besides that, if the target is an instance of Carnivore, or it is hidden its health would always equal 0 in your current version.

Animal.remove_dead_animal()

Choose a reason for hiding this comment

The 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 alive. Use .remove(target) to perform deletion in place and implement this logic inside of the bite method, delete the remove_dead_animal class method from the Animal class.



class Herbivore(Animal):

def hide(self) -> None:
self.hidden = not self.hidden