Discuss / Python / 作业

作业

Topic source

HepthST

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

def quadratic(a,b,c):
    for s in [a,b,c]:
        if not isinstance(s,(int,float)):
            raise TypeError('Error Input')
        if a != 0:
            delta = b * b - 4 * a * c
            if delta < 0:
                return 'Have no avaliable root'
            else:
                x1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
                x2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
                return 'the roots are (%f,%f)'%(x1,x2)
        else:
            return 'Error'

print(quadratic(2, 3, 1))
print(quadratic(0, 3, 1))
print(quadratic(1, 3, -4))
raw_input()

  • 1

Reply