Discuss / Python / Quiz
def quadratic(a, b, c):
    if not isinstance(a, (int, float)) or not isinstance(b, (int, float)) or not isinstance(c, (int, float)):
        print('Bad number.')
        return
    if a == 0:
        print('Bad argument.')
        return
    bsqure = b * b
    deltra = bsqure - 4 * a * c
    if deltra < 0 :
        print('No real root!')
    elif deltra == 0 :
        x = (-b)/(2 * a)
        print('This funtion has only one real root:')
        print('x = ',x)
    else :
        x1 = (-b + math.sqrt(deltra))/(2 * a)
        x2 = (-b - math.sqrt(deltra))/(2 * a)
        print('This funtion has two real roots:')
        print('x1 = ',x1)
        print('x2 = ',x2)

测试数据:1,2,1 通过 1,2,'1' 通过 1,3,1 通过 0,3,1 通过

要在最后一行加上 return x1,x2 的吧。。。

疯子_20990

#3 Created at ... [Delete] [Delete and Lock User]

def quadratic(a, b, c): z = [False for i in (a, b, c) if not isinstance(i, (int, float))] if z: raise TypeError('bad input type') delta = b*2 - 4ac x1, x2 = None, None if delta >= 0: x1 = (-b + math.sqrt(delta))/(2 a) x2 = (-b - math.sqrt(delta))/(2 * a) return x1, x2


  • 1

Reply