SyntaxError: 'break' outside loop in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# SyntaxError: 'break' outside loop in Python

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.

syntaxerror break outside loop

Here is an example of how the error occurs.

main.py
if len('hi') == 2: # ⛔️ SyntaxError: 'break' outside loop break

using break outside loop

The break statement can only be used inside of for or while loops.

main.py
# ✅ 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.

# Use a return statement to return a value from a function

If you meant to return a value from a function, use a return statement instead.

main.py
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.

If you don't need to return a specific value and just need to exit the function, simply return.
main.py
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.

# Use the sys.exit() method to exit the program

If you need to signal an intention to exit the interpreter, use the sys.exit() method.

main.py
import sys print('before') # ✅ This runs sys.exit() print('after') # ⛔️ This never runs

using sys exit

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.

# The break statement is used to exit a for or a while loop

The break statement breaks out of the innermost enclosing for or while loop.

main.py
for i in range(5): if i == 3: break print(i) # 👉️ 0 1 2 print('exited for loop')

using break to exit 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.

main.py
i = 0 while i < 5: print(i) # 👉️ 0 1 2 i += 1 if i == 3: break print('exited while loop')

using break to exit while loop

The while loop iterates until its condition returns false or the break statement is used.

# Make sure your code is correctly indented

Make sure your code is correctly indented as that often causes issues.

main.py
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.

main.py
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.

# Raising an exception to exit a code block

You can also raise an exception to exit a code block.

main.py
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.

raise exception to exit code block

# Exiting an interactive Python session

If you need to exit an interactive Python session, use the exit() function.

main.py
print('before') # ✅ This runs exit()

exit python interpreter

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.