Discuss / Python / 交作业!!!

交作业!!!

Topic source

似水年华_

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

-- coding: utf-8 --

print('方程式:aX²+bX+c=0') a=int(input('请输入参数a的值:')) b=int(input('请输入参数b的值:')) c=int(input('请输入参数c的值:')) import math def quadratic(a,b,c): d=b*2-4ac Gen=math.sqrt(d) p=-b/(2a) q=Gen/(2*a) if not (isinstance(a, (int, float)) or isinstance(b, (int, float)) or isinstance(c, (int, float))): raise TypeError('bad operand type!') if a==0: raise TypeError("a can not be '0'!") if d<0: print('该方程式无解!') elif d==0: x1=x2=p else: x1=p+q x2=p-q return x1,x2 x=quadratic(a, b, c) print('方程式:%dX²+%dX+%d=0的解为:X1=%.1f,X2=%.1f'%(a,b,c,x[0],x[1])))

西子若然

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

进行强制类型转换后,异常输入判断语句就失效了


  • 1

Reply