请定义一个函数​​quadratic(a, b, c)​​,接收3个参数,返回一元二次方程:

ax2 + bx + c = 0

的两个解。

提示:计算平方根可以调用​​math.sqrt()​​函数:

>>> import math
>>> math.sqrt(2)
1.4142135623730951

 

 

# 测试:

print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))

if quadratic(2, 3, 1) != (-0.5, -1.0):
print('测试失败')
elif quadratic(1, 3, -4) != (1.0, -4.0):
print('测试失败')
else:
print('测试成功')

 代码实现:

import math
def quadratic(a,b,c):
if pow(b,2)-4*a*c < 0:
print("无解")
else:
x1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
x2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
return x1,x2

print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))
if quadratic(2, 3, 1) != (-0.5, -1.0):
print('测试失败')
elif quadratic(1, 3, -4) != (1.0, -4.0):
print('测试失败')
else:
print('测试成功')