diff --git a/02/ex0/ft_first_exception.py b/02/ex0/ft_first_exception.py new file mode 100644 index 0000000..87b9bf6 --- /dev/null +++ b/02/ex0/ft_first_exception.py @@ -0,0 +1,27 @@ +def check_temperature(temp_str: str) -> int: + print("Testing temperature:", temp_str) + try: + x = int(temp_str) + if x > 40: + raise Exception("is too hot for plants (max 40°C)") + elif x < 0: + raise Exception("is too cold for plants (min 0°C)") + else: + print("Temperature " + temp_str + "°C is perfect for plants!") + except ValueError: + print("Error: '" + temp_str + "' is not a valid number") + pass + except Exception as ex: + print("Error: " + temp_str + "°C", ex) + pass + +if __name__ == "__main__": + check_temperature("25") + print("") + check_temperature("abc") + print("") + check_temperature("100") + print("") + check_temperature("-50") + print("") + print("All tests completed - program didn't crash!")