图1是在IDLE上运行的,图2在vscode上
为什么IDLE上只计算了分子,没有计算分母
vscode报错怎么解决
1.2*a要加括号不然/2*a会被认为是先除2再乘a
2.IDLE和vscode的终端是不同的,IDLE里面定义完一个函数可以继续调用,终端只会执行py文件内的代码,你必须在代码里面使用函数
接着写代码调用
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')
h = b*b-4*a*c
if h<0:
print('have no answer')
else:
x1 = (-b+math.sqrt(h))/2*a
x2 = (-b-math.sqrt(h))/2*a
return x1,x2
result = quadratic(1,4,2)
print(result)