Discuss / Python / 交作业

交作业

Topic source

loveKD92

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

-- coding: utf-8 --

import math def quadratic(a,b,c): if not isinstance (a,(int ,float ))and isinstance (b,(int,float))and isinstance (c,(int,float)): raise TypeError('bad operand type ') s = bb-4ac if a == 0: x1 = -c/b print ('该方程有唯一解:x= %.1f' %x1) elif c == 0: x1 = 0; x2 = -b/a print ('该方程有两个解:x1= %.1f,x2= %.1f' %(x1 ,x2))
elif s >= 0: x1 = (-b + math.sqrt(s))/(2
a); x2 = (-b - math.sqrt(s))/(2*a); print ('该方程有两个解:x1= %.1f,x2= %.1f'%(x1,x2)) else : print ('该方程无实数解')

loveKD92

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

结果是这样:

该方程有两个解:x1= -0.5,x2= -1.0 None 该方程有两个解:x1= 1.0,x2= -4.0 None

为什么会有 None,哪来的

s = bb-4ac这么写不对的,应该是s=bb-4a*c

写错了,是s=bb-4a*c


  • 1

Reply