A break statement instructs Python to exit a loop. If you use a break statement outside of a loop, for instance, in an if statement without a parent loop, you’ll encounter the “SyntaxError: ‘break’ outside loop” error in your code.
In this guide, we’re going to discuss what this error means and why you may see it. We’ll explore an example of this error so you can learn how to fix the error.
SyntaxError: ‘break’ outside loop
The Python break statement acts as a “break” in a for loop or a while loop. It stops a loop from executing for any further iterations.
Break statements are usually enclosed within an if statement that exists in a loop. In such a case, a programmer can tell a loop to stop if a particular condition is met.
A break statement can only be used inside a loop. This is because the purpose of a break statement is to stop a loop. You can use a break statement inside an if statement, but only if that if statement is inside a loop.
An Example Scenario
Let’s write a program that validates a username for a game. A username must be under twelve characters long to be valid. A username must not contain any spaces.
To validate our username, we’re going to use two if statements. To start, let’s ask a user to choose a username for our game using an input() statement:
username = input("Enter your new username: ")
Next, let’s use an if statement to check whether our username is less than 12 characters long:
if len(username) < 12: print("This username is the right number of characters.") else: break
If the username a user inserts into the program is fewer than 12 characters, our program prints a message to the console informing us that the username is of the correct length. Otherwise, a break statement will run.
Next, let’s validate whether that the username does not contain a space:
if " " not in username: print("This username is valid.") else: break
We use an if...in
statement to check for a character in the “username” string. We check for a blank space. This blank space is enclosed within the two quotation marks in our if
statement.
If a username contains a space, a break statement executes. The break statement is part of the else
statement in our code.
Let’s run our code and see what happens:
File "main.py", line 6 break ^ SyntaxError: 'break' outside loop
Our code returns an error.
The Solution
We’ve used a break statement to halt our program if one of our criteria is not met when validating a user’s username.
This causes an error because the break statement is not designed to start a break anywhere in a program. The break statement only stops a loop from executing further.
To fix our code, we need to remove the break statements. We can replace them with an exception that stops our program and provides an error message:
if len(username) < 12: print("This username is the right number of characters.") else: raise Exception("Your username must be under twelve characters.") if " " not in username: print("This username is valid.") else: raise Exception("Your username cannot contain space.s")
If the length of a username is equal to or greater than 12, or if a username contains a space, our program will raise an exception. This exception will stop our program from continuing.
Let’s run our code:
Enter your new username: bill3 This username is the right number of characters This username is valid
Our code runs successfully if our username is valid. Let’s see what happens if we enter an invalid username:
Enter your new username: bill 3 This username is the right number of characters Traceback (most recent call last): File "main.py", line 11, in <module> raise Exception("Your username cannot contain spaces") Exception: Your username cannot contain spaces
Our code returns an exception. Alternatively, we could have used print statements to inform the user that their username was incorrect. This would only be suitable if we wanted our program to continue, or if we had a loop that ran until a username validated successfully.
Conclusion
The “SyntaxError: ‘break’ outside loop” error is raised when you use a break statement outside of a loop.
To solve this error, replace any break statements with a suitable alternative. To present a user with a message, you can use a print()
statement. To halt execution of your program when a condition is met, you can raise an exception.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.