]> Untitled Git - axy/ft/python02.git/commitdiff
partial impl
authorAxy <gilliardmarthey.axel@gmail.com>
Mon, 12 Jan 2026 11:23:47 +0000 (12:23 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Mon, 12 Jan 2026 11:23:47 +0000 (12:23 +0100)
ex0/ft_first_exception.py [new file with mode: 0644]
ex1/ft_different_errors.py [new file with mode: 0644]
ex2/ft_custom_errors.py [new file with mode: 0644]
pyproject.toml [new file with mode: 0644]

diff --git a/ex0/ft_first_exception.py b/ex0/ft_first_exception.py
new file mode 100644 (file)
index 0000000..b83fed9
--- /dev/null
@@ -0,0 +1,29 @@
+def check_temperature(temp_str: str) -> None:
+    try:
+        temp = int(temp_str)
+        if temp < 0:
+            print(f"Error: {temp}°C is to cold for plants (min 0°C)")
+        elif temp > 40:
+            print(f"Error: {temp}°C is to hot for plants (max 40°C)")
+        else:
+            print(f"Temperature {temp}°C is perfect for plants!")
+    except ValueError:
+        print(f"Error: '{temp_str}' is not a valid number")
+
+
+def test_temperature_input() -> None:
+    def test_check(s: str):
+        print(f"Testing temperature: {s}")
+        check_temperature(s)
+        print()
+
+    print("=== Garden Temperature Checker ===\n")
+    test_check("25")
+    test_check("abc")
+    test_check("100")
+    test_check("-50")
+    print("All test completed - program didn't crash!")
+
+
+if __name__ == "__main__":
+    test_temperature_input()
diff --git a/ex1/ft_different_errors.py b/ex1/ft_different_errors.py
new file mode 100644 (file)
index 0000000..e20d1b8
--- /dev/null
@@ -0,0 +1,35 @@
+def garden_operations(exception: str):
+    if exception == "ValueError":
+        int("abc")
+    if exception == "ZeroDivisionError":
+        print(10 / 0)
+    if exception == "FileNotFoundError":
+        open("/dev/agilliar")
+    if exception == "KeyError":
+        {}["agilliar"]
+    print("[WARNING] No exception was raised")
+
+
+def test_error_types() -> None:
+    print("=== Garden Error Type Demo ===")
+    for error in [
+        "ValueError",
+        "ZeroDivisionError",
+        "FileNotFoundError",
+        "KeyError",
+    ]:
+        print(f"\nTesting {error}...")
+        try:
+            garden_operations(error)
+        except (
+            ValueError,
+            ZeroDivisionError,
+            FileNotFoundError,
+            KeyError,
+        ) as e:
+            print(f"Caught {type(e).__name__}: {e}")
+    print("\nAll error types tested successfully!")
+
+
+if __name__ == "__main__":
+    test_error_types()
diff --git a/ex2/ft_custom_errors.py b/ex2/ft_custom_errors.py
new file mode 100644 (file)
index 0000000..265d117
--- /dev/null
@@ -0,0 +1,25 @@
+class GardenError(Exception):
+    def __str__(self) -> str:
+        return f"General Garden Error: {super().__str__()}"
+
+
+class PlantError(GardenError):
+    plant: str
+
+    def __init__(self, plant: str) -> None:
+        self.plant = plant
+
+    def __str__(self) -> str:
+        return f"The {self.plant} plant is wilting!"
+
+
+class WaterError(GardenError):
+    def __init__(self):
+        pass
+
+    def __str__(self) -> str:
+        return "Not enough water in the tank!"
+
+
+if __name__ == "__main__":
+    raise GardenError(12)
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644 (file)
index 0000000..a8f43fe
--- /dev/null
@@ -0,0 +1,2 @@
+[tool.black]
+line-length = 79