def quadratic(a,b,c): deta = b **2 - 4 * a * c if b*b-4*a*c<0: return '无解' elif b*b-4*a*c==0: x = (-b ) / (2 * a) print('该方程只有一个解:%.2f'% x) else: x1 = (-b + math.sqrt(deta)) / (2 * a) x2 = (-b - math.sqrt(deta)) / (2 * a) print('该方程有两个解:x1=%.3f,x2=%.5f'%(x1,x2))func = quadratic(1,-5,6)print(func)
输出结果:
该方程有两个解:x1=3.000,x2=2.00000
None
?疑问:为什么会出现None值呢
你在程序的倒数第二行加入一个 return x1,x2 试试。
因为没有return的话默认是return none的。
添加 return x1,x2后的输出结果为:(3.0, 2.0),没有再出现None
还有两个疑问:
①return不能更改输出的格式么?因为我想输出【该方程有两个解:x1=3.000,x2=2.00000】这种结果
②函数竟然可以同时使用print与return表示输出,这是为什么呢?
Sign in to make a reply
oommcc
输出结果:
该方程有两个解:x1=3.000,x2=2.00000
None
?疑问:为什么会出现None值呢