作为一个用户,我应该输入什么来导致代码中出现非valueerror或zerodivisionerror#python

2024-09-30 12:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

[首先,我很抱歉我的英语不好] 我是这里的用户而不是程序员,我的老师要求我在这个程序中输入一些东西来产生一个非值错误或零除法错误,以便程序显示除"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()

Tags: the程序inputmain错误errormathroot
3条回答

如果为a输入0,将得到ZeroDivisionError

A program to find the root of the quadratic equation

a = 0

b= 3

c = 2


You are providing an input which results in zero division

如果您试图输入一些会导致ValueError的数字,您将得到第一个异常。(您需要先计算)

A program to find the root of the quadratic equation

a = 7

b= 4

c = 2

Dont have real root

对于最后一条错误消息,已经有了答案

当我在终端中运行它时,我通过按Control+C使它崩溃:

A program to find the root of the quadratic equation
a = ^C
mysterious error happen, sorry

当提示输入时,您可以尝试点击ctrl + c。 这通常会使程序退出,但由于您正在捕获所有异常,它将打印mysterious error happen, sorry

相关问题 更多 >

    热门问题