When writing Python code, you might encounter the following error:
SyntaxError: 'break' outside loop
This error occurs when you put a break
statement outside of a loop (like a for
or while
loop)
The following tutorial shows an example that causes this error and how to fix it.
How to reproduce this error
Imagine you have an if
statement that prints a variable when that variable isn’t empty:
name = "Nathan"
if name:
print("My name is", name)
Next, you put a break
statement inside that else
block as follows:
name = "Nathan"
if name:
print("My name is", name)
else:
break
When you run the code, you get the following output:
File "/main.py", line 6
break
^^^^^
SyntaxError: 'break' outside loop
The error occurs because the purpose of the break
statement is to stop the execution of a loop.
Here’s an example that uses the break
statement properly:
for i in range(7):
if i == 4:
break
print(i)
print('For loop finished running')
Output:
0
1
2
3
For loop finished running
The break
statement causes the for
loop to stop when the value of i
is equal to 4
. Without it, the loop would print the numbers from 0
to 6
.
When used outside of a loop, Python doesn’t understand what you’re trying to achieve.
How to fix this error
To resolve this error, you need to make sure that you’re not putting a break
statement outside of a loop.
Returning to the example above, if you want Python to stop executing further code in the else
statement, then you can raise an exception as follows:
name = "Nathan"
if name:
print("My name is", name)
else:
raise Exception('The variable name is empty')
This time, a proper Exception will be raised when the if
statement failed:
Traceback (most recent call last):
File "main.py", line 6, in <module>
raise Exception('The variable name is empty')
Exception: The variable name is empty
If you want the code to just stop without any output, then you can call the sys.exit()
method as follows:
import sys
name = "Nathan"
if name:
print("My name is", name)
else:
sys.exit()
This time, the else
statement simply stops the code execution without any output.
At other times, you might want to stop a function from executing any further after a condition.
In the following code, the greet()
function has a break
statement:
def greet(name):
if name:
print("Hello! I'm", name)
else:
break
greet("")
The code above will cause the same error because a break
statement must be inside a loop.
If you’re in this situation, then remove the else
statement to resolve the error:
def greet(name):
if name:
print("Hello! I'm", name)
greet("")
The function only executes when the if
statement evaluates to True
. You don’t need to use break
to stop the function because it automatically stops after the if
statement.
Conclusion
The error SyntaxError: 'break' outside loop
occurs in Python when you put a break
statement outside of a loop.
To resolve this error, you need to avoid using the break
statement outside of a loop. You can use an Exception
or the sys.exit()
method to replace the statement.
I hope this tutorial is helpful. Happy coding! 👍