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"]
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!")
"""
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...")
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:
"""
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:
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!")
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:
"""
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__":
"""
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!")
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")
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")
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__":