Corrected ex1, ready to push

This commit is contained in:
2026-01-29 12:18:19 +01:00
parent 6c5ddeebf0
commit acc6d274c2
7 changed files with 279 additions and 111 deletions

View File

@@ -1,36 +1,56 @@
class Plants:
class Plant:
name: str
height: int
p_age: int
days: int
def __init__(self, name, height, age):
self.name = name
self.height = height
self.p_age = age
def get_info(self) -> None:
"""Display plant informations"""
print(self.name, " (", self.height,
"cm, ", self.days, " days)", sep="")
def get_info(self):
print(self.name.capitalize() + ":", self.height, end="")
print("cm,", self.p_age, "days old")
def grow(self):
def grow(self) -> None:
"""Increase height"""
self.height = self.height + 1
def age(self):
self.p_age = self.p_age + 1
def age(self) -> None:
"""Increase days by one and grow plant"""
self.days = self.days + 1
self.grow()
def __init__(self, name: str, height: int, age: int) -> None:
"""Init plant with his values and display his values"""
self.name = name
self.height = height
self.days = age
print("Created: ", end="")
self.get_info()
if __name__ == "__main__":
plant_1 = Plants("Rose", 50, 2)
plant_2 = Plants("Chrysanthem", 30, 1)
plant_3 = Plants("Rosemary", 60, 5)
plant_4 = Plants("Cucumber", 40, 3)
plant_5 = Plants("Salade", 15, 4)
plants = [plant_1, plant_2, plant_3, plant_4, plant_5]
class PlantFactory:
@staticmethod
def create_plants(plants: list[tuple[str,
int, int]]) -> list[Plant | None]:
"""Create list of plants by list of arguments"""
i = 0
for n in plants:
i = i + 1
new_plants: list[Plant | None] = [None] * i
i = 0
for n in plants:
new_plants[i] = Plant(n[0], n[1], n[2])
i = i + 1
return new_plants
if __name__ == "__main__":
plants = PlantFactory.create_plants([
("Rose", 50, 2),
("Chrysanthem", 30, 1),
("Rosemary", 60, 5),
("Cucumber", 40, 3),
("Salade", 15, 4)
])
i = 0
for n in plants:
print("Created:", end=" ")
n.get_info()
i = i + 1
print("\nTotal plants created:", i)