FIX: Make 02 flake8 complient

This commit is contained in:
2026-02-02 14:41:10 +01:00
parent 7e3ca26e97
commit eab1167091
5 changed files with 35 additions and 22 deletions

View File

@@ -15,6 +15,7 @@ def check_temperature(temp_str: str) -> int | None:
print("Error: " + temp_str + "°C", ex) print("Error: " + temp_str + "°C", ex)
return (None) return (None)
if __name__ == "__main__": if __name__ == "__main__":
print("=== Garden Temperature checker ===") print("=== Garden Temperature checker ===")
print("\nTesting temperature: 25") print("\nTesting temperature: 25")

View File

@@ -1,7 +1,7 @@
def water_plants(plant_list: list[str | None]) -> None: def water_plants(plant_list: list[str | None]) -> None:
"""Display watering plant if plant is a string""" """Display watering plant if plant is a string"""
for plant in plant_list: for plant in plant_list:
if plant == None: if not plant:
raise ValueError raise ValueError
print("Watering", plant) print("Watering", plant)
@@ -14,7 +14,7 @@ def test_watering_system() -> None:
try: try:
print("Opening watering system") print("Opening watering system")
water_plants(plants) water_plants(plants)
except: except ValueError:
print("Error: Cannot water None - invalid plant!") print("Error: Cannot water None - invalid plant!")
finally: finally:
print("Closing watering system (cleanup)") print("Closing watering system (cleanup)")

View File

@@ -1,14 +1,19 @@
def check_plant_health(plant_name: str, water_level: int, sunlight_hours: int) -> None: def check_plant_health(plant_name: str, water_level: int,
sunlight_hours: int) -> None:
if not plant_name or not plant_name[0]: if not plant_name or not plant_name[0]:
raise ValueError("Error: Plant name cannot be empty") raise ValueError("Error: Plant name cannot be empty")
elif water_level < 1: elif water_level < 1:
raise ValueError(f"Error: Water level {water_level} is too low (min 1)") raise ValueError(f"Error: Water level {water_level}\
is too low (min 1)")
elif water_level > 10: elif water_level > 10:
raise ValueError(f"Error: Water level {water_level} is too high (max 10)") raise ValueError(f"Error: Water level {water_level}\
is too high (max 10)")
elif sunlight_hours < 2: elif sunlight_hours < 2:
raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too low (min 2)") raise ValueError(f"Error: Sunlight hours {sunlight_hours}\
is too low (min 2)")
elif sunlight_hours > 12: elif sunlight_hours > 12:
raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too high (max 12)") raise ValueError(f"Error: Sunlight hours {sunlight_hours}\
is too high (max 12)")
else: else:
print("Plant '" + plant_name + "' is healthy!") print("Plant '" + plant_name + "' is healthy!")

View File

@@ -34,7 +34,8 @@ class Plant:
__water_level: int __water_level: int
__sunlight_hours: int __sunlight_hours: int
def __init__(self, name: str, water_level: int, sunlight_hours: int) -> None: def __init__(self, name: str, water_level: int,
sunlight_hours: int) -> None:
if name == "": if name == "":
raise Exception("Plant name cannot be empty") raise Exception("Plant name cannot be empty")
self.set_name(name) self.set_name(name)
@@ -65,9 +66,11 @@ class Plant:
def set_sunlight_hours(self, sunlight_hours: int) -> None: def set_sunlight_hours(self, sunlight_hours: int) -> None:
if sunlight_hours < 2: if sunlight_hours < 2:
raise SunlightError(f"Sunlight hours {sunlight_hours} is too low (min 2)") raise SunlightError(f"Sunlight hours {sunlight_hours}\
is too low (min 2)")
if sunlight_hours > 12: if sunlight_hours > 12:
raise SunlightError(f"Sunlight hours {sunlight_hours} is too high (max 12)") raise SunlightError(f"Sunlight hours {sunlight_hours}\
is too high (max 12)")
self.__sunlight_hours = sunlight_hours self.__sunlight_hours = sunlight_hours
def water_plant(self) -> None: def water_plant(self) -> None:
@@ -88,7 +91,6 @@ class GardenManager:
def __str__(self) -> str: def __str__(self) -> str:
return f"Caught WaterTankError: {self.message}" return f"Caught WaterTankError: {self.message}"
def __init__(self) -> None: def __init__(self) -> None:
self.garden = [] self.garden = []
self.__water_tank_level = 0 self.__water_tank_level = 0
@@ -115,15 +117,20 @@ class GardenManager:
try: try:
for plant in self.garden: for plant in self.garden:
if plant.get_water_level() < 1: if plant.get_water_level() < 1:
raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too low (min 1)") raise WaterError(f"{plant.get_name()}: Water level\
{plant.get_water_level()} is too low (min 1)")
elif plant.get_water_level() > 10: elif plant.get_water_level() > 10:
raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too high (max 10)") raise WaterError(f"{plant.get_name()}: Water level\
{plant.get_water_level()} is too high (max 10)")
elif plant.get_sunlight_hours() < 2: elif plant.get_sunlight_hours() < 2:
raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too low (min 2)") raise SunlightError(f"{plant.get_name()}: Sunlight hours\
{plant.get_sunlight_hours()} is too low (min 2)")
elif plant.get_sunlight_hours() > 12: elif plant.get_sunlight_hours() > 12:
raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too high (max 12)") raise SunlightError(f"{plant.get_name()}: Sunlight hours\
{plant.get_sunlight_hours()} is too high (max 12)")
else: else:
print(f"{plant.get_name()}: healthy (water: {plant.get_water_level()}, sun: {plant.get_sunlight_hours()})") print(f"{plant.get_name()}: healthy (water:\
{plant.get_water_level()}, sun: {plant.get_sunlight_hours()})")
except (WaterError, SunlightError) as err: except (WaterError, SunlightError) as err:
print(err) print(err)