Last updated: Apr 8, 2024
Reading time·4 min
The Python "SyntaxError: 'break' outside loop" occurs when we use the break
statement outside of a loop.
To solve the error, use a return
statement to return a value from a
function, or use the sys.exit()
method to exit the interpreter.
Here is an example of how the error occurs.
if len('hi') == 2: # ⛔️ SyntaxError: 'break' outside loop break
The break statement can only be used inside of for or while loops.
# ✅ Using break inside a for loop for i in range(5): if i == 3: break print(i) # 👉️ 0 1 2 print('exited for loop')
The code sample above uses a break
statement inside a for
loop to exit the
loop once the condition is met.
return
statement to return a value from a functionIf you meant to return a value from a function, use a return statement instead.
def example(): if len('hi') == 2: return 100 return 0 result = example() print(result) # 👉️ 100
The return
statement can be used to return a value and exit the function.
return
.def example(): if len('hi') == 2: return # 👈️ Return to exit return 0 result = example() print(result) # 👉️ None
The code sample above uses a return
statement to exit the function in the if
block.
sys.exit()
method to exit the programIf you need to signal an intention to exit the interpreter, use the sys.exit()
method.
import sys print('before') # ✅ This runs sys.exit() print('after') # ⛔️ This never runs
The sys.exit method can be used to signal an intention to exit the interpreter.
The last line from the code sample is never run.
break
statement is used to exit a for
or a while
loopThe break statement breaks out of the
innermost enclosing for
or while
loop.
for i in range(5): if i == 3: break print(i) # 👉️ 0 1 2 print('exited for loop')
The if
statement checks if the i
variable stores a value of 3
, and if it
does, we break out of the for
loop.
Here is an example of using the break
statement in a while
loop.
i = 0 while i < 5: print(i) # 👉️ 0 1 2 i += 1 if i == 3: break print('exited while loop')
The while
loop iterates until its condition returns false or the break
statement is used.
Make sure your code is correctly indented as that often causes issues.
for i in range(5): if i == 3: print(i) break # ⛔️ error
The break
statement is not indented and therefore is not a part of the for
loop, so trying to use it causes the error.
Instead, indent the break
statement in the for
loop using tabs or spaces.
for i in range(5): if i == 3: break print(i) # 👉️ 0 1 2 print('exited for loop')
Now the break
statement is a part of the for
loop and can be used without
any issues.
Make sure to only indent your code using tabs or spaces.
Using tabs and spaces to indent a line often causes issues.
You can also raise an exception to exit a code block.
an_int = 10 if an_int > 5: raise Exception('example error message') else: print('bar')
The code sample raises an Exception
if the condition is met.
Any code below the raise
statement won't run because raising an error causes
the program to exit.
If you need to exit an interactive Python session, use the exit()
function.
print('before') # ✅ This runs exit()
You can learn more about the related topics by checking out the following tutorials: