88e1a8dmodule-02: add ex1
1 file changed, +32, -0
@@ -0,0 +1,32 @@
1+def input_temperature(temp_str: str) -> int:
2+ temp = int(temp_str)
3+
4+ if temp > 40:
5+ raise ValueError(f"{temp}°C is too hot for plants (max 40°C)")
6+
7+ if temp < 0:
8+ raise ValueError(f"{temp}°C is too cold for plants (min 0°C)")
9+
10+ return temp
11+
12+def test_temperature() -> None:
13+ print("=== Garden Temperature ===\n")
14+
15+ test_inputs = ["25", "abc", "100", "-50"]
16+
17+ for tmp in test_inputs:
18+ print(f"Input data is '{tmp}'")
19+
20+ try:
21+ temperature = input_temperature(tmp)
22+ print(f"Temperature is now {temperature}°C")
23+ except ValueError as error:
24+ print(f"Caught input_temperature error: {error}")
25+
26+ print()
27+
28+ print("All tests completed - program didn't crash!")
29+
30+
31+if __name__ == "__main__":
32+ test_temperature()