# 定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程
# ax ^ 2 + bx + c = 0
# 计算平方根可以调用math.sqrt()函数
import math
# print(help(math.sqrt))
def quadratic(a,b,c):
s = (b ** 2) - (4 * a * c)
if a == 0 :
return '此方程无解'
elif s < 0:
return '此方程无解'
else:
sq = math.sqrt(s)
x1 = (-b + sq)/(2 * a)
x2 = (-b -sq)/(2 * a)
if x1 == x2: #或者换成 if s == 0:
if abs(x1) % 1 == 0:
return 'x1=x2=%d' % (x1)
else:
return 'x1=x2=%.2f' % (x1)
elif abs(x1) % 1 == 0 and abs(x2) % 1 == 0:
return 'x1=%d,x2=%d' % (x1,x2)
else:
return 'x1=%.2f,x2=%.2f' % (x1,x2)
print(quadratic(1,2,1))