[首先,我很抱歉我的英语不好]
我是这里的用户而不是程序员,我的老师要求我在这个程序中输入一些东西来产生一个非值错误或零除法错误,以便程序显示除"mysterious error happened, sorry"
之外的最后一个。我应该输入什么
import math
def main():
print('A program to find the root of the quadratic equation')
try:
a = float(input('a = '))
b = float(input('b= '))
c = float(input('c = '))
discriminant = b * b - 4 * a * c
squareDisc = math.sqrt(discriminant)
root1 = (-b + squareDisc) / (2 * a)
root2 = (-b - squareDisc) / (2 * a)
print(' x1 =', root1)
print(' x2 =', root2)
except ValueError as obyekExcept:
if str(obyekExcept) == 'math domain error':
print('\nDont have real root')
else:
print('\nYou give non number input')
except ZeroDivisionError:
print('\nYou are providing an input which results in zero division')
except:
print("\nmysterious error happen, sorry")
main()
如果为
a
输入0
,将得到ZeroDivisionError
如果您试图输入一些会导致
ValueError
的数字,您将得到第一个异常。(您需要先计算)对于最后一条错误消息,已经有了答案
当我在终端中运行它时,我通过按
Control+C
使它崩溃:当提示输入时,您可以尝试点击
ctrl + c
。 这通常会使程序退出,但由于您正在捕获所有异常,它将打印mysterious error happen, sorry
相关问题 更多 >
编程相关推荐