python -- SyntaxError :'return' outside function

在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 []

 

 

你可能感兴趣的:(notes)