一元二次方程的定义是:ax² + bx + c = 0
Python的math包提供了sqrt()函数用于计算平方根。
# coding:utf-8
import math
def func(a, b, c):
d = b**2 - 4 * a *c
if d >= 0:
num = math.sqrt(d)
x1 = (-b + num) / (2 * a)
x2 = (-b - num) / (2 * a)
return x1, x2
else:
return
print func(2, 3, 0)
print func(1, -6, 5)