python 练习题

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

ax2 + bx + c = 0

的两个解。

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

import math
def quadratic_equation(a, b, c):
t = math.sqrt(pow(b, 2) - 4 * a * c)
if(pow(b, 2) - 4 * a * c) > 0:
return (-b + t) / (2 * a), (-b - t) / (2 * a)
elif (pow(b, 2) - 4 * a * c) == 0:
return (-b + t) / (2 * a)
else:
return None
print quadratic_equation(4, 4, -8)

 

 

附一元二次方程推导

https://blog.csdn.net/hengbao4/article/details/73030664

posted @ 2018-06-20 16:55  虎皮猫  阅读(182)  评论(0)    收藏  举报