How To Solve “SyntaxError: ‘Break’ Outside Loop” In Python

How To Resolve SyntaxError: ‘Break’ Outside Loop In Python

The error “SyntaxError: ‘break’ outside loop” happens when you don’t put a ‘break’ statement in the loop. To fix the error, let follow the article below.

What causes the error “SyntaxError: ‘break’ outside loop” in Python?

Example: 

myList = ["learnshareit", "google", "stackoverflow"]

# 'break' is in the wrong place
for s in myList:
    print(s)
break

Output:

Traceback (most recent call last):
File "prog.py", line 4
    break
    ^^^^^
SyntaxError: 'break' outside loop

How to solve this error?

Put the ‘break’ statement in the right place

The simplest way to solve that error is to put a ‘break’ statement inside the loop.

Example:

  • Create a list.
  • Using a for loop, I only want to print out one element in the list, so after the print statement, I use a ‘break’ statement to finish.
myList = ["learnshareit", "google", "stackoverflow"]

# Put 'break' inside the for loop
for s in myList:
    print(s)
    break
    print(s)

Output:

learnshareit

Use sys.exit() function

In Python, the ‘sys’ module has a built-in function sys.exit() that exits the program.

Syntax:

sys.exit()

Example:

  • Import module ‘sys’.
  • Create a list.
  • Using a for loop, I only want to print out one element in the list, so after the print statement, I use the sys.exit() function to finishing.
import sys

myList = ["learnshareit", "google", "stackoverflow"]

# Use the sys.exit() function to finishing
for s in myList:
    print(s)
    sys.exit()
    print(s)

Output:

learnshareit

Use exit() function

You can also use the exit() function to end the program. Note that the exit() function only works when the ‘site’ module is imported, so you should only use it in the compiler. The sys.exit() function is still more optimal if you use it to end the program.

Syntax:

exit()

Example:

  • Create a list.
  • Using a for loop, I only want to print out one element in the list, so after the print statement, I use the exit() function to finishing.
myList = ["learnshareit", "google", "stackoverflow"]

# Use the exit() function to finishing
for s in myList:
    print(s)
    exit()
    print(s)

Use os.exit() function

A function that also has a function to exit the program is the os._exit() function. But this function is used to exit when an immediate exit is required. It will not clear the cache. Call the cleanup handler so that I would recommend still using the sys.exit() function.

Syntax:

os._exit()

Steps:

  • Create a list.
  • Using a for loop, I only want to print out one element in the list, so after the print statement, I use the os._exit() function to finishing.

Example:

import os

myList = ["learnshareit", "google", "stackoverflow"]

# Use the os._exit() function to finishing.
for s in myList:
    print(s)
    os._exit(0)

Output:

visit

It would be best if you imported the ‘os’ module because the os._exit() function is in it. I do not print any value and end the program.

Summary

If you have any questions about the “SyntaxError: ‘break’ outside loop” error in Python, please leave a comment below. I will answer your questions as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *