Discuss / Python / 模仿一楼大神

模仿一楼大神

Topic source

GhZicE-奇

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

感谢一楼大神的 local().values() 学习了一个新参数

import math

def checkType(n):
    for i in n:
        if not isinstance(i,(int, float)):
            return False
    return True

def quadratic(a, b, c):
    if not checkType(locals().values()):
        return TypeError ("u MUST input int or float type")

    if a == 0:
        return ("a CAN NOT be 0")

    delta = math.sqrt(b*b - 4*a*c)

    x1 = (-b + delta) / (2*a)
    x2 = (-b - delta) / (2*a)

    if x1 == x2:
        return x1
    else:
        return x1, x2

print(quadratic(2, 3, 1))
print(quadratic(1, 3, -4))
print(quadratic("A", 3, -4))
print(quadratic(0, 3, -4))

  • 1

Reply