]> Untitled Git - axy/ft/python01.git/commitdiff
up to ex 3
authorAxy <gilliardmarthey.axel@gmail.com>
Tue, 6 Jan 2026 14:54:33 +0000 (15:54 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Tue, 6 Jan 2026 14:54:33 +0000 (15:54 +0100)
ex0/ft_garden_intro.py
ex1/ft_garden_data.py
ex2/ft_plant_growth.py [new file with mode: 0644]
ex3/ft_plant_factory.py [new file with mode: 0644]

index e1fe6e0d850d5d7b70198db20c56e587c8c69494..87d2926e4df0bf22c828c6581ffa30cc8a058ed3 100644 (file)
@@ -1,7 +1,9 @@
 if __name__ == "__main__":
-    name: str = "Rose"
+    print("=== Welcome to My Garden ===")
+    name: str = "rose"
     height_cm: int = 25
     age_days: int = 30
-    print(f"Plant: {name}")
+    print(f"Plant: {name.capitalize()}")
     print(f"Height: {height_cm}cm")
     print(f"Age: {age_days} days")
+    print("\n=== End of Program ===")
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..206d32f6cf055ff654624c3083abbf49d588917e 100644 (file)
@@ -0,0 +1,22 @@
+class Plant:
+    def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+        self.name = name
+        self.height_cm = height_cm
+        self.age_days = age_days
+
+    def __repr__(self) -> str:
+        return (
+            f"{self.name.capitalize()}: "
+            + f"{self.height_cm}cm, "
+            + f"{self.age_days} days old"
+        )
+
+
+if __name__ == "__main__":
+    print("=== Garden Plant Registry ===")
+    rose = Plant("rose", 25, 30)
+    sunflower = Plant("sunflower", 80, 45)
+    cactus = Plant("cactus", 15, 120)
+    print(rose)
+    print(sunflower)
+    print(cactus)
diff --git a/ex2/ft_plant_growth.py b/ex2/ft_plant_growth.py
new file mode 100644 (file)
index 0000000..1fefef0
--- /dev/null
@@ -0,0 +1,31 @@
+class Plant:
+    def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+        self.name = name
+        self.height_cm = height_cm
+        self.age_days = age_days
+
+    def __repr__(self) -> str:
+        return (
+            f"{self.name.capitalize()}: "
+            + f"{self.height_cm}cm, "
+            + f"{self.age_days} days old"
+        )
+
+    def grow(self, by_cm: int):
+        self.height_cm += by_cm
+
+    def age(self, by_days: int):
+        # We assume a growth rate of 1cm per day
+        self.grow(by_days)
+        self.age_days += by_days
+
+
+if __name__ == "__main__":
+    print("=== Day 1 ===")
+    rose = Plant("rose", 25, 30)
+    initial_height = rose.height_cm
+    print(rose)
+    rose.age(6)
+    print("=== Day 7 ===")
+    print(rose)
+    print(f"Growth this week: +{rose.height_cm - initial_height}cm")
diff --git a/ex3/ft_plant_factory.py b/ex3/ft_plant_factory.py
new file mode 100644 (file)
index 0000000..5f1bb25
--- /dev/null
@@ -0,0 +1,45 @@
+class Plant:
+    def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+        self.name = name
+        self.height_cm = height_cm
+        self.age_days = age_days
+
+    def __repr__(self) -> str:
+        return (
+            f"{self.name.capitalize()}: "
+            + f"{self.height_cm}cm, "
+            + f"{self.age_days} days old"
+        )
+
+    def grow(self, by_cm: int):
+        self.height_cm += by_cm
+
+    def age(self, by_days: int):
+        # We assume a growth rate of 1cm per day
+        self.grow(by_days)
+        self.age_days += by_days
+
+
+class PlantFactory:
+    def __init__(self):
+        self.plants = []
+
+    def create(self, name: str, height_cm: int, age_days: int) -> Plant:
+        plant = Plant(name, height_cm, age_days)
+        print(
+            f"Created: {plant.name.capitalize()} "
+            f"({plant.height_cm}cm, {plant.age_days} days)"
+        )
+        self.plants += [plant]
+        return plant
+
+
+if __name__ == "__main__":
+    print("=== Plant Factory Output ===")
+    factory = PlantFactory()
+    factory.create("rose", 25, 30)
+    factory.create("oak", 200, 365)
+    factory.create("cactus", 5, 90)
+    factory.create("sunflower", 80, 45)
+    factory.create("fern", 15, 120)
+    print(f"\nTotal Plants created: {len(factory.plants)}")