Discuss / Python / 交作业

交作业

Topic source

import math def quadratic(a,b,c): if not isinstance(a,(int,float)): print('Typrerror!') return 65536 if not isinstance(b,(int,float)): print('Typrerror!') return 65536 if not isinstance(c,(int,float)): print('Typrerror!') return 65536 if a == 0: print('int a can\'t be 0 value!') return 65536 elif ((bb-4ac) < 0): print('have no solutions!') return 65536 elif ((bb-4ac) == 0): x = (-b)/(2a) return x else: x1 = (-b+math.sqrt(bb-4ac))/(2a) x2 = (-b-math.sqrt(bb-4ac))/(2*a) return x1,x2 a,b,c = eval(input('please input a,b,c:')) print('a = %d,b = %d,c = %d'%(a,b,c)) r = quadratic(a,b,c) print(r) if(isinstance(r,tuple)):#判断返回值是否为tuple类型,是则为双根 print('x1 = %f,x2 = %f' %(r[0],r[1])) else: #不是tuple类型,是int类型
if(r == 65536): #错误 print('error') else: print('x1 = x2 =',r)


  • 1

Reply