When running the following code (in Python 2.7.1 on a mac with Mac OS X 10.7)
while True: return False
I get the following error
SyntaxError: 'return' outside function
I've carefully checked for errant tabs and/or spaces. I can confirm that the code fails with the above error when I use the recommended 4 spaces of indentation. This behavior also happens when the return is placed inside of other control statements (e.g. if, for, etc.).
Any help would be appreciated. Thanks!
return has no meaning outside of a function, and so python raises an error. Show activity on this post. You are not writing your code inside any function, you can return from functions only. Remove return statement and just print the value you want.
This syntax error is nothing but a simple indentation error, generally, this error occurs when the indent or return function does not match or align to the indent of the defined function. As you can see that line no.
Say your function bar is in a file called foo.py on your Python path. Then you can do this: from foo import bar if bar(): print "bar() is True!"
Simply call a function to pass the output into the second function as a parameter will use the return value in another function python.
The return statement only makes sense inside functions:
def foo(): while True: return False
Use quit()
in this context. break
expects to be inside a loop, and return
expects to be inside a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With