SyntaxError: 'return' outside function / 'break' outside loop (tested)
SyntaxError: 'return' outside function and 'break' outside loop mean the statement isn't where Python thinks you meant it to be, almost always due to indentation. We measured each case on Python 3.13.5.
1. 'return' outside function: almost always indentation
def total(items):
s = 0
for x in items:
s += x
return s
Output:
File "/tmp/rb/b1.py", line 5
return s
^^^^^^^^
SyntaxError: 'return' outside function
Measured on Python 3.13.5: the return is at column 0, so Python sees it as module-level code after the function ended. Indent it to the function body. If the indentation looks right, the file may mix tabs and spaces; we checked that case and Python 3 reports TabError: inconsistent use of tabs and spaces in indentation instead, so this error really means the line is outside the def.
2. 'break' outside loop: same cause
items = [3, 8, -1, 5]
for x in items:
if x < 0:
print("negative found")
break
Output:
File "/tmp/rb/b2.py", line 5
break
^^^^^
SyntaxError: 'break' outside loop
Measured: the break was dedented out of the for. (continue in the same position gives 'continue' not properly in loop, checked with compile().)
3. break inside a helper function called from a loop
def find_negative(items):
def check(x):
if x < 0:
break
for x in items:
check(x)
Output:
File "/tmp/rb/b3.py", line 4
break
^^^^^
SyntaxError: 'break' outside loop
Measured: break only applies to a loop in the same function. A helper can't break its caller's loop; return a value instead and break in the loop based on it.
4. Correct placement
def first_negative(items):
for x in items:
if x < 0:
return x # inside the def: return works
return None
print(first_negative([3, 8, -1, 5]))
for x in [3, 8, -1, 5]:
if x < 0:
print("negative found:", x)
break # inside the loop: break works
else:
print("none found")
Output:
-1
negative found: -1
return belongs inside the def; break inside the loop. The for ... else runs only if the loop didn't break.
5. Wanted to stop a script early? Use sys.exit()
A top-level return is often an attempt to stop the script. That's sys.exit():
import sys
if len(sys.argv) < 2:
sys.exit("usage: b5.py NAME")
print("hello", sys.argv[1])
Output:
usage: b5.py NAME
Measured: run with no arguments, it printed the message to stderr and exited with status 1 (sys.exit with a string does both).
Frequently asked questions
How do I fix SyntaxError: 'return' outside function?
Indent the return so it's inside the def's body. We measured a return at column 0 after a function raising exactly this.
What does SyntaxError: 'break' outside loop mean?
The break isn't inside a for or while in the same function: either it's dedented out of the loop, or it's in a helper function called from the loop. We measured both.
How do I exit a Python script early without return?
Use sys.exit(). We measured sys.exit('usage: ...') printing the message to stderr and exiting with status 1.
Can mixed tabs and spaces cause 'return' outside function?
Not on Python 3: we measured mixed indentation raising TabError instead. 'return' outside function means the line really is outside the def.
Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: a dedented return, a dedented break, break inside a helper function, continue at module level, mixed tabs and spaces, correct placement with for/else, and sys.exit with a message. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.