I'm getting points for the ZeroDivisionError exception but not the ValueError exception....not sure what's wrong with my exception statement. It looks correct to my noob eyes. Any help is appreciated.
LAB: Simple integer division - multiple exception handlers
Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError and output an exception message. Use another except block to catch any ValueError caused by invalid input and output an exception message.
Note: ZeroDivisionError is thrown when a division by zero happens. ValueError is thrown when a user enters a value of different data type than what is defined in the program. Do not include code to throw any exception in the program.
Ex: If the input of the program is:
15
3
the output of the program is:
5
Ex: If the input of the program is:
10
0
the output of the program is:
Zero Division Exception: integer division or modulo by zero
Ex: If the input of the program is:
15.5
5
the output of the program is:
Input Exception: invalid literal for int() with base 10: '15.5'
My code:
user_num = int(input())
div_num = int(input())
if isinstance(user_num,int) == False:
problem = user_num
elif isinstance(div_num,int) == False:
problem = div_num
try:
result = user_num/div_num
print(int(result))
except ZeroDivisionError:
print("Zero Division Exception: integer division or modulo by zero")
except ValueError:
print("Input Exception: invalid literal for int() with base 10: '{}'".format(problem))
Enter program input (optional)
15.5
5
Program errors displayed here
Traceback (most recent call last):
File "main.py", line 1, in <module>
user_num = int(input())
ValueError: invalid literal for int() with base 10: '15.5'
1: Compare output 2 / 2
Input
15
3
Your output
5
2: Compare output 2 / 2
Input
15
0
Your output
Zero Division Exception: integer division or modulo by zero
3: Compare output 0 / 2
Traceback (most recent call last):
File "main.py", line 1, in <module>
user_num = int(input())
ValueError: invalid literal for int() with base 10: '15.5'
Input
15.5
5
Your output Your program produced no output
Expected output
Input Exception: invalid literal for int() with base 10: '15.5'
4: Compare output 0 / 1
Traceback (most recent call last):
File "main.py", line 2, in <module>
div_num = int(input())
ValueError: invalid literal for int() with base 10: '0.5'
Input
25
0.5
Your output
Your program produced no output
Expected output
Input Exception: invalid literal for int() with base 10: '0.5'
5: Compare output 0 / 1
Traceback (most recent call last):
File "main.py", line 1, in <module>
user_num = int(input())
ValueError: invalid literal for int() with base 10: 'twenty'
Input
twenty
5
Your output
Your program produced no output
Expected output
Input Exception: invalid literal for int() with base 10: 'twenty'
6: Compare output 1 / 1
Input
0
4
Your output
0
7: Compare output 1 / 1
Input
15
0
Your output
Zero Division Exception: integer division or modulo by zero
CodePudding user response:
user_num = int(input())
and div_num = int(input())
need to go in a try/except ValueError
block because the call to int()
is where the error can occur.
You can put the whole thing in try/except
and catch multiple exceptions:
try:
user_num = int(input())
div_num = int(input())
result = user_num/div_num
print(int(result))
except ValueError as e1:
print(e1)
except ZeroDivisionError as e2:
print(e2)
Or even:
try:
user_num = int(input())
div_num = int(input())
result = user_num/div_num
print(int(result))
except (ValueError, ZeroDivisionError) as e:
print(e)