Jul-16-2017, 05:55 PM
(Jul-16-2017, 04:10 PM)kethyar Wrote: I might need to tell the program to treat the input as a number?Yes,that's the first step.
So if input is
a
or 1.0
,get a error.>>> input_capacity = int(input("Enter the number of gallons or liters used: ")) Enter the number of gallons or liters used: a
Error:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
invalid literal for int() with base 10: 'a'
This ValueError
can be catch and give a more useful message to user.def capacity_prompt(): while True: try: input_capacity = int(input("Enter the number of gallons or liters used: ")) return input_capacity except ValueError: print('Only number as input,try again') print(capacity_prompt())If you run it,you see that it will only return out integer.
You use the same method with divide by zero error.
>>> 7 / 0
Error:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
division by zero