bdazg1994 2020-07-25 21:45 采纳率: 100%
浏览 82
已采纳

请教一下这个python代码哪里错误

定义求解一元二次方程的根的函数哪里错了,运行后一直提示定义函数语法错误

import math

def quadratic(a,b,c)
    if not isinstance(a,b,c,(int,float)):
       raise TypeError('bad operand type')
    d = b**2-4*a*c
    if d > 0:
       x1 = (-b+math.sqrt(d))/(2*a)
       x2 = (b+math.sqrt(d))/(2*a) 
    elif d = 0:
       x1 = -b/(2*a)
       x2 = x1
    else 
         return False    
x1,x2 = quadratic(2,3,1)
print(x1,x2)

  • 写回答

1条回答 默认 最新

  • 白小斗 2020-07-25 22:54
    关注

    修改如下,供参考:

    import math
    
    def quadratic(a,b,c):
        if isinstance((a,b,c),(int,int,int)):
           raise TypeError('bad operand type')
        d = b**2-4*a*c
        if d > 0:
            x1 = (-b+math.sqrt(d))/(2*a)
            x2 = (b+math.sqrt(d))/(2*a) 
        elif d == 0:
            x1 = -b/(2*a)
            x2 = x1
        else:
            return False
        return x1, x2
    
    x1,x2 = quadratic(2,3,1)
    print(x1,x2)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?