Discuss / Python / 另外考虑了虚数解的情况,但无返回值

另外考虑了虚数解的情况,但无返回值

Topic source

诏屎孤儿

#1 Created at ... [Delete] [Delete and Lock User]

import math

def quadratic(a,b,c):

print('The function is %dX^2+%dX+%d=0' % (a,b,c))

deta = b * b - 4 * a * c

if deta > 0:
    x1 = (-b + math.sqrt(deta)) / (2 * a)
    x2 = (-b - math.sqrt(deta)) / (2 * a)
    print('The solutions are %f and %f:'%(x1,x2))
elif deta == 0:
    x = -b / (2 * a)
    print('The solution is %f:' % x)
else:
    xi = -b / (2 * a)
    xj1 = math.sqrt(-deta)/ (2*a)
    xj2 = -math.sqrt(-deta)/ (2*a)
    print('The solutions are %f+%fi and %f-%fi'%(xi,xj1,xi,xj2))

  • 1

Reply