From 14b0bdae020005cf851e9b344e20ad02e0296d7b Mon Sep 17 00:00:00 2001 From: Maksym Date: Thu, 5 Dec 2024 22:33:54 +0200 Subject: [PATCH 1/7] herbivores-and-carnivores --- README.md | 7 ++++--- app/main.py | 44 +++++++++++++++++++++++++++++++++++++++++++- tests/test_main.py | 12 ++++++------ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5b58b7e6..09769cec 100644 --- a/README.md +++ b/README.md @@ -30,19 +30,20 @@ Create a `Сarnivore` class. This class should inherit from Animal. Carnivore has a `bite` method, which takes a herbivore object and decreases the object's health by 50. The method does not work if it is another сarnivore, or the herbivore is currently hiding. + ```python lion = Carnivore("Lion King") rabbit = Herbivore("Susan") rabbit.health == 100 -lion.bite(rabbit) +bite(rabbit) rabbit.health == 50 # bited rabbit.hide() -lion.bite(rabbit) +bite(rabbit) rabbit.health == 50 # lion cannot bite hidden rabbit rabbit.hide() -lion.bite(rabbit) +bite(rabbit) rabbit.health == 0 # rabbit is dead rabbit in Animal.alive # False diff --git a/app/main.py b/app/main.py index fa56336e..5c4032ec 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,43 @@ -# write your code here +class Animal: + def __init__(self, name, health=100): + self.name = name + self.health = health + self.hidden = False + + def take_damage(self, damage): + self.health -= damage + if self.health < 0: + self.health = 0 + print(f"{self.name} took {damage} damage. Health: {self.health}") + + def is_alive(self): + return self.health > 0 + + def __str__(self): + return f"Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}" + +class Herbivore(Animal): + def __init__(self, name, health=100): + self.name = name + self.health = health + self.hidden = False + + def hide(self): + self.hidden = not self.hidden + print(f"{self.name} is now {'hidden' if self.hidden else 'visible'}.") + +class Carnivore(Animal): + def __init__(self, name, health=100): + self.name = name + self.health = health + self.hidden = False + + def bite(self, herbivore) -> None: + if isinstance(herbivore, Herbivore): + if herbivore.hidden: + print(f"{self.name} cannot bite {herbivore.name} because they are hidden.") + else: + herbivore.take_damage(50) + print(f"{self.name} bit {herbivore.name}. {herbivore.name}'s health: {herbivore.health}") + else: + print(f"{self.name} cannot bite another carnivore.") diff --git a/tests/test_main.py b/tests/test_main.py index dcdf42cd..eb61db80 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -70,7 +70,7 @@ def test_carnivore_bite_not_hidden(): Animal.alive = [] lion = Carnivore("King Lion") rabbit = Herbivore("Susan") - lion.bite(rabbit) + bite(rabbit) assert rabbit.health == 50, ( "If initial health of rabbit equals 100 and rabbit is not hidden " "health should equal to 50 after bite." @@ -82,7 +82,7 @@ def test_carnivore_bite_hidden(): lion = Carnivore("King Lion") rabbit = Herbivore("Susan") rabbit.hide() - lion.bite(rabbit) + bite(rabbit) assert rabbit.health == 100, ( "Carnivore cannot bite hidden herbivore" ) @@ -93,8 +93,8 @@ def test_carnivore_bite_to_death(): lion = Carnivore("King Lion") pantera = Carnivore("Bagira") rabbit = Herbivore("Susan") - lion.bite(rabbit) - pantera.bite(rabbit) + bite(rabbit) + bite(rabbit) assert len(Animal.alive) == 2, ( f"It shouldn't be dead animals in Animals.alive" ) @@ -103,7 +103,7 @@ def test_carnivore_bite_to_death(): def test_carnivore_bite_carnivore(): lion = Carnivore("Simba") pantera = Carnivore("Bagire") - lion.bite(pantera) + bite(pantera) assert pantera.health == 100 @@ -146,7 +146,7 @@ def test_when_health_less_than_zero(): Animal.alive = [] lion = Carnivore("King Lion") rabbit = Herbivore("Susan", 25) - lion.bite(rabbit) + bite(rabbit) assert len(Animal.alive) == 1, ( "Herbivore should die if health less than zero" ) From 402d366a7772a4e5cb7ab1fdeeef478b527cf0a2 Mon Sep 17 00:00:00 2001 From: Maksym Date: Thu, 5 Dec 2024 23:00:14 +0200 Subject: [PATCH 2/7] herbivores-and-carnivores --- app/main.py | 48 +++++++++++++++++++++++++++------------------- tests/test_main.py | 12 ++++++------ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/main.py b/app/main.py index 5c4032ec..06833f9d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,43 +1,51 @@ class Animal: - def __init__(self, name, health=100): + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: self.name = name self.health = health self.hidden = False + Animal.alive.append(self) - def take_damage(self, damage): + def take_damage(self, damage: int) -> None: self.health -= damage if self.health < 0: self.health = 0 print(f"{self.name} took {damage} damage. Health: {self.health}") - def is_alive(self): + if self.health == 0: + self.die() + + def die(self) -> None: + if self in Animal.alive: + Animal.alive.remove(self) + print(f"{self.name} is dead.") + + def is_alive(self) -> bool: return self.health > 0 - def __str__(self): - return f"Name: {self.name}, Health: {self.health}, Hidden: {self.hidden}" + def __repr__(self) -> str: + return (f"{{Name: {self.name}, " + f"Health: {self.health}, " + f"Hidden: {self.hidden}}}" + ) -class Herbivore(Animal): - def __init__(self, name, health=100): - self.name = name - self.health = health - self.hidden = False - def hide(self): +class Herbivore(Animal): + def hide(self) -> None: self.hidden = not self.hidden - print(f"{self.name} is now {'hidden' if self.hidden else 'visible'}.") + print(f"{self.name} is now {"hidden" if self.hidden else "visible"}.") -class Carnivore(Animal): - def __init__(self, name, health=100): - self.name = name - self.health = health - self.hidden = False - def bite(self, herbivore) -> None: +class Carnivore(Animal): + def bite(self, herbivore: Herbivore) -> None: if isinstance(herbivore, Herbivore): if herbivore.hidden: - print(f"{self.name} cannot bite {herbivore.name} because they are hidden.") + print(f"{self.name} cannot bite {herbivore.name} " + f"because they are hidden.") else: herbivore.take_damage(50) - print(f"{self.name} bit {herbivore.name}. {herbivore.name}'s health: {herbivore.health}") + print(f"{self.name} bit {herbivore.name}. " + f"{herbivore.name}'s health: {herbivore.health}") else: print(f"{self.name} cannot bite another carnivore.") diff --git a/tests/test_main.py b/tests/test_main.py index eb61db80..dcdf42cd 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -70,7 +70,7 @@ def test_carnivore_bite_not_hidden(): Animal.alive = [] lion = Carnivore("King Lion") rabbit = Herbivore("Susan") - bite(rabbit) + lion.bite(rabbit) assert rabbit.health == 50, ( "If initial health of rabbit equals 100 and rabbit is not hidden " "health should equal to 50 after bite." @@ -82,7 +82,7 @@ def test_carnivore_bite_hidden(): lion = Carnivore("King Lion") rabbit = Herbivore("Susan") rabbit.hide() - bite(rabbit) + lion.bite(rabbit) assert rabbit.health == 100, ( "Carnivore cannot bite hidden herbivore" ) @@ -93,8 +93,8 @@ def test_carnivore_bite_to_death(): lion = Carnivore("King Lion") pantera = Carnivore("Bagira") rabbit = Herbivore("Susan") - bite(rabbit) - bite(rabbit) + lion.bite(rabbit) + pantera.bite(rabbit) assert len(Animal.alive) == 2, ( f"It shouldn't be dead animals in Animals.alive" ) @@ -103,7 +103,7 @@ def test_carnivore_bite_to_death(): def test_carnivore_bite_carnivore(): lion = Carnivore("Simba") pantera = Carnivore("Bagire") - bite(pantera) + lion.bite(pantera) assert pantera.health == 100 @@ -146,7 +146,7 @@ def test_when_health_less_than_zero(): Animal.alive = [] lion = Carnivore("King Lion") rabbit = Herbivore("Susan", 25) - bite(rabbit) + lion.bite(rabbit) assert len(Animal.alive) == 1, ( "Herbivore should die if health less than zero" ) From e56f0ac0c8eb49e63d0a61d77a1eb423f7e9f7b6 Mon Sep 17 00:00:00 2001 From: Maksym Date: Fri, 6 Dec 2024 22:34:30 +0200 Subject: [PATCH 3/7] herbivores-and-carnivores --- app/main.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..06833f9d 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,51 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + self.hidden = False + Animal.alive.append(self) + + def take_damage(self, damage: int) -> None: + self.health -= damage + if self.health < 0: + self.health = 0 + print(f"{self.name} took {damage} damage. Health: {self.health}") + + if self.health == 0: + self.die() + + def die(self) -> None: + if self in Animal.alive: + Animal.alive.remove(self) + print(f"{self.name} is dead.") + + def is_alive(self) -> bool: + return self.health > 0 + + 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 + print(f"{self.name} is now {"hidden" if self.hidden else "visible"}.") + + +class Carnivore(Animal): + def bite(self, herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore): + if herbivore.hidden: + print(f"{self.name} cannot bite {herbivore.name} " + f"because they are hidden.") + else: + herbivore.take_damage(50) + print(f"{self.name} bit {herbivore.name}. " + f"{herbivore.name}'s health: {herbivore.health}") + else: + print(f"{self.name} cannot bite another carnivore.") From 949c159728edea9bd7611dbc81f42a39c5588ca6 Mon Sep 17 00:00:00 2001 From: Maksym Date: Sat, 7 Dec 2024 14:56:53 +0200 Subject: [PATCH 4/7] herbivores-and-carnivores --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 06833f9d..fbb9c257 100644 --- a/app/main.py +++ b/app/main.py @@ -46,6 +46,7 @@ def bite(self, herbivore: Herbivore) -> None: else: herbivore.take_damage(50) print(f"{self.name} bit {herbivore.name}. " - f"{herbivore.name}'s health: {herbivore.health}") + f"{herbivore.name} s health: {herbivore.health}" + ) else: print(f"{self.name} cannot bite another carnivore.") From 01330307d74adef4f18b3ba93687b391358fc217 Mon Sep 17 00:00:00 2001 From: Maksym Date: Sat, 7 Dec 2024 15:13:01 +0200 Subject: [PATCH 5/7] herbivores-and-carnivores --- app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main.py b/app/main.py index fbb9c257..ac05ef06 100644 --- a/app/main.py +++ b/app/main.py @@ -34,7 +34,6 @@ def __repr__(self) -> str: class Herbivore(Animal): def hide(self) -> None: self.hidden = not self.hidden - print(f"{self.name} is now {"hidden" if self.hidden else "visible"}.") class Carnivore(Animal): From 1d6bdd67fadb99b43ccae4a44c252b43b101ffa8 Mon Sep 17 00:00:00 2001 From: Maksym Date: Sat, 7 Dec 2024 17:56:10 +0200 Subject: [PATCH 6/7] herbivores-and-carnivores --- app/main.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..ac05ef06 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,51 @@ -# write your code here +class Animal: + alive = [] + + def __init__(self, name: str, health: int = 100) -> None: + self.name = name + self.health = health + self.hidden = False + Animal.alive.append(self) + + def take_damage(self, damage: int) -> None: + self.health -= damage + if self.health < 0: + self.health = 0 + print(f"{self.name} took {damage} damage. Health: {self.health}") + + if self.health == 0: + self.die() + + def die(self) -> None: + if self in Animal.alive: + Animal.alive.remove(self) + print(f"{self.name} is dead.") + + def is_alive(self) -> bool: + return self.health > 0 + + 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): + def bite(self, herbivore: Herbivore) -> None: + if isinstance(herbivore, Herbivore): + if herbivore.hidden: + print(f"{self.name} cannot bite {herbivore.name} " + f"because they are hidden.") + else: + herbivore.take_damage(50) + print(f"{self.name} bit {herbivore.name}. " + f"{herbivore.name} s health: {herbivore.health}" + ) + else: + print(f"{self.name} cannot bite another carnivore.") From 938c5ff7b59c11f4af2b3f861297bdb46994de60 Mon Sep 17 00:00:00 2001 From: Maksym Date: Sat, 7 Dec 2024 21:09:45 +0200 Subject: [PATCH 7/7] herbivores-and-carnivores --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 09769cec..2d9ddd83 100644 --- a/README.md +++ b/README.md @@ -35,15 +35,15 @@ does not work if it is another сarnivore, or the herbivore is currently hiding. lion = Carnivore("Lion King") rabbit = Herbivore("Susan") rabbit.health == 100 -bite(rabbit) +lion.bite(rabbit) rabbit.health == 50 # bited rabbit.hide() -bite(rabbit) +lion.bite(rabbit) rabbit.health == 50 # lion cannot bite hidden rabbit rabbit.hide() -bite(rabbit) +lion.bite(rabbit) rabbit.health == 0 # rabbit is dead rabbit in Animal.alive # False