Discuss / Python / 根据楼下的兄弟,稍稍加了个返回值,就测试成功了

根据楼下的兄弟,稍稍加了个返回值,就测试成功了

Topic source

    # 添加一步检查参数,如果不是参数或者浮点型参数,就报错

    if not isinstance(a,(int,float)) or not isinstance(b,(int,float)) or not isinstance(c,(int,float) ):

        raise TypeError("bad operand type")

    dart = b**2 - 4*a*c

    if a==0:

        print("此方程无意义")

    elif dart < 0:

        print("此方程无解")

    elif dart == 0:

        x = -b/(2*a)

        print(f'此方程的解为:x1=x2={x:.1f}')

        return x

    else:

        x1 = (-b + math.sqrt(dart))/(2*a)

        x2 = (-b - math.sqrt(dart))/(2*a)

        print(f'此方程的解为:x1={x1:.1f},x2={x2:.1f}')

        return x1,x2


  • 1

Reply