Python函数之返回多值一元二次方程的定义是:ax² + bx + c = 0请参考求根公式:x = (-b±√(b²-4ac)) / 2a
1#import math def quadratic_equation(a, b, c): de=b**2-4*a*c if de>=0: x1=(-b+math.sqrt(de))/(2*a) x2=(-b-math.sqrt(de))/(2*a) return x1,x2 else: return print quadratic_equation(2, 3, 0) print quadratic_equation(1, -6, 5) 3#import math def quadratic_equation(a, b, c): t = math.sqrt(b * b - 4 * a * c) return (-b + t) / (2 * a),( -b - t )/ (2 * a) print quadratic_equation(2, 3, 0) print quadratic_equation(1, -6, 5) 2#import math def quadratic_equation(a, b, c): if (b*b-4*a*c)<0: return "no real root" else: x=b*b-4*a*c return (-b+math.sqrt(x))/(2*a),(-b-math.sqrt(x))/(2*a) print quadratic_equation(2, 3, 0) print quadratic_equation(1, -6, 5)
一元二次方程虽然我现在只上高中知道怎么在作业本上解但是在python完全不能理解def quadratic_equation(a, b, c):下面的计算过程谁能帮我理解一下?
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
这3个代码里面不变的是什么?
计算公式吧#de=b**2-4*a*c
if de>=0:
x1=(-b+math.sqrt(de))/(2*a)
x2=(-b-math.sqrt(de))/(2*a)
return x1,x2
#def quadratic_equation(a, b, c):
t = math.sqrt(b * b - 4 * a * c)
return (-b + t) / (2 * a),( -b - t )/ (2 * a)
#def quadratic_equation(a, b, c):
if (b*b-4*a*c)<0:
return "no real root"
else:
x=b*b-4*a*c
return (-b+math.sqrt(x))/(2*a),(-b-math.sqrt(x))/(2*a)