Discuss / Python / 提交下作业

提交下作业

Topic source

ohgod99

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

提交下代码,应该是这样写的,公式百度的,都忘光了- -

def Quadratic(a, b, c):
    import math;
    if not isintance(a, (int)):
        raise TypeError('bad oprand type');
    if not isintance(b, (int)):
        raise TypeError('bad oprand type');
    if not isintance(c, (int)):
        raise TypeError('bad oprand type');

    print('%d*x^2 + (%d*x) + (%d)'%(a, b, c));

    #a = 0的情况
    if a == 0:
        return -c/b;

    #判断方程式有几个实数根
    z = b*b - 4*a*c;

    #无实数根
    if z < 0:
        return '无实数根.';
    #有一个实数根
    elif z == 0:
        y = (-b + math.sqrt(z))/(2*a);
           return y;
    #有两个实数根
    elif z > 0:
        y = (-b + math.sqrt(z))/(2*a);
        x = (-b - math.sqrt(z))/(2*a);
        return x, y;
    return ;

print(Quadratic(1, -4, 4));

  • 1

Reply