]> Untitled Git - axy/ft/python02.git/commitdiff
touchups master
authorAxy <gilliardmarthey.axel@gmail.com>
Tue, 20 Jan 2026 14:27:27 +0000 (15:27 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Tue, 20 Jan 2026 14:27:27 +0000 (15:27 +0100)
ex0/ft_first_exception.py
ex1/ft_different_errors.py
ex2/ft_custom_errors.py
ex3/ft_finally_block.py
ex4/ft_raise_errors.py
ex5/ft_garden_management.py

index 61787ca848a8703aa9d66b891bd64c90ee430704..d67abaeeea4ba94b9fc1acd1c3674674f6852919 100644 (file)
@@ -7,22 +7,20 @@ def check_temperature(temp_str: str) -> int | None:
         temp = int(temp_str)
         if temp < 0:
             print(f"Error: {temp}°C is too cold for plants (min 0°C)")
-            return None
         elif temp > 40:
             print(f"Error: {temp}°C is too hot for plants (max 40°C)")
-            return None
         else:
             return temp
     except ValueError:
         print(f"Error: '{temp_str}' is not a valid number")
-        return None
+    return None
 
 
 def test_temperature_input() -> None:
     """
     Demonstrates testing with various inputs.
     """
-    print("=== Garden Temperature Checker ===")
+    print("=== Garden Temperature Checker ===\n")
 
     inputs = ["25", "abc", "100", "-50"]
 
@@ -31,6 +29,7 @@ def test_temperature_input() -> None:
         result = check_temperature(inp)
         if result is not None:
             print(f"Temperature {result}°C is perfect for plants!")
+        print()
 
     print("All tests completed - program didn't crash!")
 
index a6f88b1f993ea560da100e41079bf13333f4ffbf..3c347668d84404337fb8aa09adc2bf6a2fec8f96 100644 (file)
@@ -2,28 +2,28 @@ def garden_operations() -> None:
     """
     Demonstrates handling of different exception types in garden operations.
     """
-    print("=== Garden Error Types Demo ===")
+    print("=== Garden Error Types Demo ===\n")
 
     # ValueError
     print("Testing ValueError...")
     try:
         int("abc")
     except ValueError as e:
-        print(f"Caught ValueError: {e}")
+        print(f"Caught ValueError: {e}\n")
 
     # ZeroDivisionError
     print("Testing ZeroDivisionError...")
     try:
         10 / 0
     except ZeroDivisionError as e:
-        print(f"Caught ZeroDivisionError: {e}")
+        print(f"Caught ZeroDivisionError: {e}\n")
 
     # FileNotFoundError
     print("Testing FileNotFoundError...")
     try:
         open("missing.txt", "r")
-    except FileNotFoundError:
-        print("Caught FileNotFoundError: No such file 'missing.txt'")
+    except FileNotFoundError as e:
+        print(f"Caught FileNotFoundError: {e}\n")
 
     # KeyError
     print("Testing KeyError...")
@@ -31,14 +31,14 @@ def garden_operations() -> None:
         d = {}
         _ = d["missing_plant"]
     except KeyError as e:
-        print(f"Caught KeyError: {e}")
+        print(f"Caught KeyError: {e}\n")
 
     # Multiple errors
     print("Testing multiple errors together...")
     try:
         int("abc")
     except (ValueError, ZeroDivisionError):
-        print("Caught an error, but program continues!")
+        print("Caught an error, but program continues!\n")
 
 
 def test_error_types() -> None:
index 1917cd5fc38ee74689e9b43f479fe3c4ebe4a474..529daada8cae8ba46f6a8d4d1fdce4af350a0660 100644 (file)
@@ -20,19 +20,19 @@ def test_custom_errors() -> None:
     """
     Demonstrates raising and catching custom exceptions.
     """
-    print("=== Custom Garden Errors Demo ===")
+    print("=== Custom Garden Errors Demo ===\n")
 
     print("Testing PlantError...")
     try:
         raise PlantError("The tomato plant is wilting!")
     except PlantError as e:
-        print(f"Caught PlantError: {e}")
+        print(f"Caught PlantError: {e}\n")
 
     print("Testing WaterError...")
     try:
         raise WaterError("Not enough water in the tank!")
     except WaterError as e:
-        print(f"Caught WaterError: {e}")
+        print(f"Caught WaterError: {e}\n")
 
     print("Testing catching all garden errors...")
     try:
@@ -43,7 +43,7 @@ def test_custom_errors() -> None:
     try:
         raise WaterError("Not enough water in the tank!")
     except GardenError as e:
-        print(f"Caught a garden error: {e}")
+        print(f"Caught a garden error: {e}\n")
 
     print("All custom error types work correctly!")
 
index de5d73b1d108ea5b96710f9d282390c4850409fb..49a07f0b133d130ec6090d2e195ad3a8d281d0ea 100644 (file)
@@ -5,7 +5,7 @@ def water_plants(plant_list: list) -> None:
     try:
         print("Opening watering system")
         for plant in plant_list:
-            if not isinstance(plant, str):
+            if plant is None:
                 raise TypeError(f"Cannot water {plant} - invalid plant!")
             print(f"Watering {plant}")
     except TypeError as e:
@@ -18,15 +18,15 @@ def test_watering_system() -> None:
     """
     Tests the watering system with valid and invalid lists.
     """
-    print("=== Garden Watering System ===")
+    print("=== Garden Watering System ===\n")
 
     print("Testing normal watering...")
     water_plants(["tomato", "lettuce", "carrots"])
-    print("Watering completed successfully!")
+    print("Watering completed successfully!\n")
 
     print("Testing with error...")
     water_plants(["tomato", None, "carrots"])
-    print("Cleanup always happens, even with errors!")
+    print("\nCleanup always happens, even with errors!")
 
 
 if __name__ == "__main__":
index f9e66ba05b29a226828f47723b749bc848ae0624..3af0519dfbff48f8ed3cee942dcd37ff2603e7a9 100644 (file)
@@ -31,31 +31,20 @@ def test_plant_checks() -> None:
     """
     Tests plant health check with various invalid inputs.
     """
-    print("=== Garden Plant Health Checker ===")
-
-    print("Testing good values...")
-    try:
-        print(check_plant_health("tomato", 5, 8))
-    except ValueError as e:
-        print(f"Error: {e}")
-
-    print("Testing empty plant name...")
-    try:
-        check_plant_health("", 5, 8)
-    except ValueError as e:
-        print(f"Error: {e}")
-
-    print("Testing bad water level...")
-    try:
-        check_plant_health("lettuce", 15, 8)
-    except ValueError as e:
-        print(f"Error: {e}")
-
-    print("Testing bad sunlight hours...")
-    try:
-        check_plant_health("carrot", 5, 0)
-    except ValueError as e:
-        print(f"Error: {e}")
+    print("=== Garden Plant Health Checker ===\n")
+
+    values: list[tuple[str, str, int, int]] = [
+        ("good values", "tomato", 5, 8),
+        ("empty plant name", "", 5, 8),
+        ("bad water level", "lettuce", 15, 8),
+        ("bad sunlight hours", "carrot", 5, 0),
+    ]
+    for testing, plant, water, sun in values:
+        print(f"Testing {testing}...")
+        try:
+            print(check_plant_health(plant, water, sun))
+        except ValueError as e:
+            print(f"Error: {e}\n")
 
     print("All error raising tests completed!")
 
index 731e95dc7b017926e9ec5988e3c48e726d413749..a4bf43bb8fed848957a3c80c7cb704de83fefb60 100644 (file)
@@ -85,7 +85,7 @@ def test_garden_management() -> None:
     print("=== Garden Management System ===")
     manager = GardenManager()
 
-    print("Adding plants to garden...")
+    print("\nAdding plants to garden...")
     try:
         manager.add_plant("tomato")
         manager.add_plant("lettuce")
@@ -93,14 +93,14 @@ def test_garden_management() -> None:
     except PlantError as e:
         print(f"Error adding plant: {e}")
 
-    print("Watering plants...")
+    print("\nWatering plants...")
     manager.water_plants()
 
-    print("Checking plant health...")
+    print("\nChecking plant health...")
     manager.check_plant_health("tomato", 5, 8)
     manager.check_plant_health("lettuce", 15, 8)
 
-    print("Testing error recovery...")
+    print("\nTesting error recovery...")
     try:
         # Simulate a critical error condition we want to catch explicitly
         raise GardenError("Not enough water in tank")
@@ -108,7 +108,7 @@ def test_garden_management() -> None:
         print(f"Caught GardenError: {e}")
         print("System recovered and continuing...")
 
-    print("Garden management system test complete!")
+    print("\nGarden management system test complete!")
 
 
 if __name__ == "__main__":