Discuss / Python / 交作业

交作业

Topic source

dudefitslol

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

def quadratic(a,b,c):

    for lp in [a,b,c]:

        if not isinstance(lp,(int,float)):

            raise TypeError('bad operand type')

    if a == 0:

        raise TypeError("'a' cannot be 0")

    elif (b**2-4*a*c) < 0:

        raise TypeError('no real number root')

    dlt=pow((b**2-4*a*c),1/2)

    m1 = (-b+dlt)/(2*a)

    m2 = (-b-dlt)/(2*a)

    return m1,m2

卑微小唐

#2 Created at ... [Delete] [Delete and Lock User]
 if not isinstance(a, int) or not isinstance(b, int) or not isinstance(c, int):
        raise TypeError('bad operand type')
 num = b**2-4*a*c
 deno = 2*a
 if num != 0 and deno !=0 :
    temp = math.sqrt(num)
    x = (-b+temp)/deno
    y = (-b-temp)/deno
    return x,y
 else:
    return "被开方数或分母不能为0"

  • 1

Reply