问题是
对于ax 2 + bx + c形式的二次方程, 判别式,D是b 2 -4ac。 编写一个函数,根据判别式返回以下输出。
- D> 0:2真正的根源。
- D = 0:1真根。
- D< 0:2复杂的根。
实施例
>>> quadratic(1, 2, 3) 'This equation has 2 complex roots.' >>> quadratic(1, 3, 2) 'This equation has 2 real roots.' >>> quadratic(1, 4, 4) 'This equation has 1 real root.'
Python给出了“私有测试用例失败”错误。我的错误在哪里?
def quadrtic(a,b,c):
d=b**2-4*a*c
if d<0:
return "This equation has 2 complex roots."
elif d==1:
return "This equation has 2 real roots."
elif d==0 or d==1:
return "This equation has 1 real root."
答案 0 :(得分:1)
您的if
块应
def quadrtic(a,b,c):
d = b**2 - 4*a*c
if d < 0:
return "This equation has 2 complex roots."
elif d > 0:
return "This equation has 2 real roots."
else: # d == 0
return "This equation has 1 real root."
判别式不太可能完全是== 1
。因为它有两个真正的根,它必须大于0,在这种情况下判别式可以是任何实数(例如4.2564)
答案 1 :(得分:0)
D =(b ** 2)-(4 * a * c)
return('The discriminant is {0}.'.format(D))