From: Axy Date: Tue, 20 Jan 2026 14:27:27 +0000 (+0100) Subject: touchups X-Git-Url: https://git.uwuaxy.net/?a=commitdiff_plain;ds=inline;p=axy%2Fft%2Fpython02.git touchups --- diff --git a/ex0/ft_first_exception.py b/ex0/ft_first_exception.py index 61787ca..d67abae 100644 --- a/ex0/ft_first_exception.py +++ b/ex0/ft_first_exception.py @@ -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!") diff --git a/ex1/ft_different_errors.py b/ex1/ft_different_errors.py index a6f88b1..3c34766 100644 --- a/ex1/ft_different_errors.py +++ b/ex1/ft_different_errors.py @@ -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: diff --git a/ex2/ft_custom_errors.py b/ex2/ft_custom_errors.py index 1917cd5..529daad 100644 --- a/ex2/ft_custom_errors.py +++ b/ex2/ft_custom_errors.py @@ -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!") diff --git a/ex3/ft_finally_block.py b/ex3/ft_finally_block.py index de5d73b..49a07f0 100644 --- a/ex3/ft_finally_block.py +++ b/ex3/ft_finally_block.py @@ -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__": diff --git a/ex4/ft_raise_errors.py b/ex4/ft_raise_errors.py index f9e66ba..3af0519 100644 --- a/ex4/ft_raise_errors.py +++ b/ex4/ft_raise_errors.py @@ -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!") diff --git a/ex5/ft_garden_management.py b/ex5/ft_garden_management.py index 731e95d..a4bf43b 100644 --- a/ex5/ft_garden_management.py +++ b/ex5/ft_garden_management.py @@ -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__":