mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-01-27 01:01:59 +00:00
02/00
This commit is contained in:
27
02/ex0/ft_first_exception.py
Normal file
27
02/ex0/ft_first_exception.py
Normal file
@@ -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!")
|
||||||
Reference in New Issue
Block a user