在for循环里面return想要跳出全部循环时,会报SyntaxError: 'return' outside function
,也就是语法错误
for x in range(3):
print(x)
for c in ['a', 'b', 'c']:
print(c)
if c == 'b':
return []
# SyntaxError: 'return' outside function
原因是return只能写在def函数里面, 即
def test():
for x in range(3):
print(x)
for c in ['a', 'b', 'c']:
print(c)
if c == 'b':
return []
另外,break在多重循环中,只能break当前那一层循环
参考链接:https://stackoverflow.com/questions/7842120/python-return-statement-error-return-outside-function