summaryrefslogtreecommitdiff
path: root/py02/ex1/ft_raise_exception.py
blob: 7f330453eac668a943a6d855e799ac9fc4e3d972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
# https://docs.python.org/3/tutorial/errors.html#handling-exceptions

def input_temperature(temp_str: str) -> int:
    print(f" Input data is '{temp_str}'")
    temp = test_temperature(temp_str)
    return temp


def test_temperature(temp):
    try:
        temp = int(temp)
        if temp > 40:
            raise ValueError(f"{temp}°C is too hot for plants (max 40°C)")
        elif temp < 0:
            raise ValueError(f"{temp}°C is too cold for plants (min 0°C)")
    except ValueError as ve:
        print(" Caught input_temperature error:", ve)
    else:
        print(f" Temperature is now {temp}°C")
        return temp


if __name__ == "__main__":
    print("=== Garden Temperature ===")
    print()
    input_temperature("25")
    print()
    input_temperature("abc")
    print()
    input_temperature("100")
    print()
    input_temperature("-50")
    print()
    print(" All tests completed - program didn't crash!")