Python ';返回到函数'之外;缩进是否正确?

Python ';返回到函数'之外;缩进是否正确?,python,Python,在python中,它带有“函数外返回”,我检查缩进是否正确。有线索吗 dict={1:10,2:20,3:30} for a,b in dict.items(): if b==30: return a 没有函数,因此不能使用return。您可以在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,因为

在python中,它带有“函数外返回”,我检查缩进是否正确。有线索吗

dict={1:10,2:20,3:30}

for a,b in dict.items():
  if b==30:
    return a

没有函数,因此不能使用return。您可以在
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
循环的
不是函数。
python中的函数是使用
def
关键字定义的,如下所示:

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.

功能在哪里?