Tags: python, Time: 2018-04-17 08:00:00

正在学python,主要在看廖雪峰的python教程,讲到函数定义的时候,有道课外习题:求ax2+bx+c=0的解,折腾了下还是写出来了,把过程记录一下。

1、 公式推导

51321523951342074

2、 python实现

import math

def quadratic(a, b, c):
    if a == 0 and b == 0 and c != 0:
        return "此方程无解"
    if a == 0 and b == 0 and c == 0:
        return 'x为任意值'
    if a == 0 and b != 0:
        return c / b

    x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
    x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)

    if x1 == x2:
        return "x为%s" % x1
    else:
        return "x为%s或者%s" % (x1, x2)

3、参考

廖雪峰的python教程