Python 异常处理程序不工作,我不明白为什么

Python exception handler not working and I can't see why

我得到了 ZeroDivisionError 异常的分数,但不是 ValueError 异常....不确定我的异常语句有什么问题。在我的菜鸟眼中它看起来是正确的。感谢任何帮助。

LAB:简单整数除法 - 多个异常处理程序

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'

我的代码:

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:比较输出 2 / 2

输入

15
3

你的输出

5

2:比较输出 2 / 2

输入

15
0

你的输出

Zero Division Exception: integer division or modulo by zero

3:比较输出 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'

输入

15.5
5

你的输出 你的程序没有输出

预期输出

Input Exception: invalid literal for int() with base 10: '15.5'

4:比较输出 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'

输入

25
0.5

你的输出

Your program produced no output

预期输出

Input Exception: invalid literal for int() with base 10: '0.5'

5:比较输出 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'

输入

twenty
5

你的输出

Your program produced no output

预期输出

Input Exception: invalid literal for int() with base 10: 'twenty'

6:比较输出 1 / 1

输入

0
4

你的输出

0

7:比较输出 1 / 1

输入

15
0

你的输出

Zero Division Exception: integer division or modulo by zero

user_num = int(input())div_num = int(input()) 需要进入 try/except ValueError 块,因为对 int() 的调用可能会发生错误。

您可以将整个内容放入 try/except 并捕获多个异常:

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)

甚至:

try:
    user_num = int(input())
    div_num = int(input())
    result = user_num/div_num
    print(int(result))
except (ValueError, ZeroDivisionError) as e:
    print(e)