-6
在python中,它带出'返回函数外部',我检查缩进没有错。任何线索?“返回功能错误”错误与正确的缩进?
dict={1:10,2:20,3:30}
for a,b in dict.items():
if b==30:
return a
在python中,它带出'返回函数外部',我检查缩进没有错。任何线索?“返回功能错误”错误与正确的缩进?
dict={1:10,2:20,3:30}
for a,b in dict.items():
if b==30:
return a
没有功能,所以你不能使用返回。您可能会包裹代码def
:
d={1:10,2:20,3:30}
def return_30(d):
for a,b in d.items():
if b==30:
return a
而且我改名dict
到d
,因为dict
是该类型的名称,当你重新定义它,你无法访问原始dict
。
for
循环,不是函数。
def function():
print(1+2) # Im inside the function, return keyword here is valid
# I'm outside the function.
for x in range(10):
print(x)
#This is not a function, return keyword here is invalid.
哪儿功能: Python中的函数使用
def
关键字中定义? –