计算简单一元二次方程解

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

ax^2 + bx + c = 0

的两个解。

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

import math
def quadratic(a,b,c):
p=b*b-4*a*c
if p>=0 and a!=0:
x1=(-b+math.sqrt(p))/(2*a)
x2=(-b-math.sqrt(p))/(2*a)
return x1,x2
else:
return('Wrong Number!')

a=float(input('Please input a'))
b=float(input('Please input b'))
c=float(input('Please input c'))
print(quadratic(a,b,c))
原文地址:https://www.cnblogs.com/yuandatougo/p/5010472.html