Discuss / Python / 练习

练习

Topic source

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

import math

def quadratic(i,b,l):

    if i==0:

        return "Error:'i' should not be zero"

    elif not isinstance(i,(int,float)) or not isinstance(b,(int,float)) or not isinstance(l,(int,float)):

        return "Error:Input should be numbers"

    else:

        discriminant=b**2-4*i*l

        if discriminant<0:

            return "No real roots"

        elif discriminant==0:

            root = -b/(2*i)

            return root

        else:

            root1=(-b+math.sqrt(discriminant))/(2*i)

            root2=(-b-math.sqrt(discriminant))/(2*i)

            return root1,root2

print(quadratic(1,9,3))

def quadratic(a, b, c):    import math    delta = b*b - 4*a*c    if delta > 0:        print("方程有两个不同的解,x1=%f, x2 = %f\n" % (((-b) + math.sqrt(delta))/2*a,((-b) - math.sqrt(delta))/2*a))    elif delta == 0:        print("方程有两个相同的解,x1 = x2 = %f\n" % (-b)/2*a)    else:        print("该方程无解!\n")

  • 1

Reply