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/ex1/ft_raise_exception.py | |
First commit, add all files
Diffstat (limited to 'py02/ex1/ft_raise_exception.py')
| -rwxr-xr-x | py02/ex1/ft_raise_exception.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/py02/ex1/ft_raise_exception.py b/py02/ex1/ft_raise_exception.py new file mode 100755 index 0000000..7f33045 --- /dev/null +++ b/py02/ex1/ft_raise_exception.py @@ -0,0 +1,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!") |
