diff options
| author | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
|---|---|---|
| committer | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
| commit | 15115b4c52bfda0d1cca9fa1155beecbb873ec35 (patch) | |
| tree | b3f0975e63eb04dcba732a78ce9bd9abda8acf01 /py02/ex4 | |
First commit, add all files
Diffstat (limited to 'py02/ex4')
| -rwxr-xr-x | py02/ex4/ft_finally_block.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/py02/ex4/ft_finally_block.py b/py02/ex4/ft_finally_block.py new file mode 100755 index 0000000..b029636 --- /dev/null +++ b/py02/ex4/ft_finally_block.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +class GardenError(Exception): + def __init__(self, message="Unknown plant error"): + self.message = message + # Why self.message; why not just 'message' + super().__init__(self.message) + + +class PlantError(GardenError): + pass + + +class WaterError(GardenError): + pass + + +def water_plant(plant): + if plant == plant.capitalize(): + print(f"Watering {plant}: [OK]") + else: + raise PlantError(f"Invalid plant name to water: '{plant}'") + + +def test_watering_system(plants): + print("Open watering system") + try: + for plant in plants: + water_plant(plant) + except PlantError as err: + print("Caught PlantError:", err) + print("...ending tests and returning to main") + finally: + print("Closing watering system") + + +if __name__ == "__main__": + print("=== Garden Watering System ===") + print() + print("Testing valid plants...") + test_watering_system(["Tomato", "Lettuce", "Carrots"]) + print() + print("Testing invalid plants...") + test_watering_system(["Tomato", "lettuce", "Carrots"]) + print() + print("Cleanup always happens, even with errors!") |
