一元二次函数的编程:
def quadratic(a,b,c):
... xh=b*b-4*a*c
... if xh < 0:
... return 0
... print('方程无解')
... elif xh >= 0:
... q1 = (-b+math.sqrt(xh))/(2*a)
... q2 = (-b-math.sqrt(xh))/(2*a)
... return q1,q2
... print('方程解 =',q1,q2)
# 测试
... print('quadratic(2,3,1)=',quadratic(2,3,1))
quadratic(2,3,1)= (-0.5, -1.0)
>>> print('quadratic(1,3,4)=',quadratic(1,3,4))
quadratic(1,3,4)= 0