Discuss / Python / 摸索中度过

摸索中度过

Topic source

import math def quadratic(a, b, c): if not isinstance(a, (int, float)): raise TypeError('bad operand type') if not isinstance(b, (int, float)): raise TypeError('bad operand type') if not isinstance(c, (int, float)): raise TypeError('bad operand type') deta = b*2 - 4ac if(deta < 0): print('此方程无解!') elif(deta == 0): x = (-b+deta)/(2a) print('方程有两个相同的解!x1 = x2 = %.2f'%x) else: x1 = (-b+math.sqrt(deta))/(2a) x2 = (-b-math.sqrt(deta))/(2a) print('方程有两个不同的解!x1 = %.3f, x2 = %.2f'%(x1,x2))


  • 1

Reply