-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
py-herbivores-and-carnivores Solution
- Loading branch information
Showing
1 changed file
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
# write your code here | ||
class Animal: | ||
alive = [] | ||
|
||
def __init__(self, | ||
name: str, | ||
health: int = 100, | ||
hide: bool = False) -> None: | ||
self.name = name | ||
self.health = health | ||
self.hidden = hide | ||
Animal.alive.append(self) | ||
|
||
def death(self) -> None: | ||
Animal.alive.remove(self) | ||
|
||
def __repr__(self) -> str: | ||
return (f"{{Name: {self.name}," | ||
f" Health: {self.health}," | ||
f" Hidden: {self.hidden}}}") | ||
|
||
|
||
class Herbivore(Animal): | ||
def hide(self) -> None: | ||
self.hidden = not self.hidden | ||
|
||
|
||
class Carnivore(Animal): | ||
@staticmethod | ||
def bite(victim: "Herbivore") -> None: | ||
if isinstance(victim, Herbivore) and not victim.hidden: | ||
victim.health -= 50 | ||
if victim.health <= 0: | ||
victim.death() |