Discuss / Python / 提交作业啦o((>ω< ))o

提交作业啦o((>ω< ))o

Topic source

参数检查

L = (a,b,c)
for x in L:
    if not isinstance(x,(int,float)):
        raise TypeError('bad operand type')

检查是否存在解

if b * b - 4 * a * c < 0:
    print('no solution','\n')

求解

elif  b * b - 4 * a * c == 0:
    print('This equation has only one solution:','\n',(math.sqrt(b*b-4*a*c)-b)/(2*a),'\n')
else:
    print('This equation has two solutions:','\n')
    print('x1 = ',(-b+math.sqrt(b*b-4*a*c))/(2*a),'\n'+'x2 = ',(-b-math.sqrt(b*b-4*a*c))/(2*a),'\n')

  • 1

Reply