Tag Archives: SyntaxError: ‘break’ outside loop

Python Error: SyntaxError: ‘break’ outside loop [How to Fix]

When running, there is an error: syntax error: ‘break’ outside loop

Reason: break can only be used in for and while loops

Specific examples of error reporting

>>> def func(L):
...     result = {}
...     if not isinstance(L,list):
...         print("Incorrect type")
...         break
...
  File "<stdin>", line 5
SyntaxError: 'break' outside loop

Solution:

>>> def func(L):
...     result = {}
...     while not isinstance(L,list):
...         print("Incorrect type")
...         break
...
>>> func([1,2,3,4])
>>> func("qwe")
Incorrect type