请教大家一个问题
Topic source找到原因了,是因为return语句没有缩进,else没有执行,但是return语句依然执行,没了else,x1,x2就没有了根据。只要把return降级即可。作业修改如下:
import math
def quadratic(a,b,c):
for x in (a,b,c):
if not isinstance(x,(int,float)):
raise TypeError('is not a operand type')
else:
z=b**2-4*a*c
if z<0:
print('there is no result')
else:
x1=(-b+math.sqrt(z))/2/a
x2=(-b-math.sqrt(z))/2/a
return x1,x2
- 1
用户6174369584
大家有没有遇到过这个情况,下面这个函数定义后报错,显示“local variable 'x1' referenced before assignment”
import math
def quadratic(a,b,c):
if not isinstance(a,(int,float)):
raise TypeError('is not a operand type')
if not isinstance(b,(int,float)):
raise TypeError('is not a operand type')
if not isinstance(c,(int,float)):
raise TypeError('is not a operand type')
z=b**2-4*a*c
if z<0:
print('there is no result')
else:
x1=(-b+math.sqrt(z))/2/a
x2=(-b-math.sqrt(z))/2/a
return x1,x2