TryBugHunt

SyntaxError: 'continue' not properly in loop

SyntaxError: 'continue' not properly in loop in Python means python found a continue that is not inside a for or while loop. Python checks this before running anything, so the whole file fails to start.

Python

How do I fix SyntaxError: 'continue' not properly in loop?

SyntaxError: 'continue' not properly in loop means Python found a continue that is not inside a for or while loop body. Usually the line is indented too little, or it sits inside a def that is called from a loop. Indent continue into the loop body. Inside a function, use return. Elsewhere, use an if/else.

  1. 1. Check the indentationThe continue must be indented further than a for or while line above it.
  2. 2. Look for a defIf a def sits between the loop and the continue, the continue is outside the loop. Use return instead.
  3. 3. Replace with if/elseWith no loop at all, wrap the code to skip in if condition: so it only runs when needed.

What does SyntaxError: 'continue' not properly in loop mean?

continue means "skip the rest of this loop round and start the next one". It only makes sense inside the body of a loop. If the line is outside every loop, often because of indentation or because it sits in a separate function, Python stops with this error before running a single line. The same mistake with break gives "SyntaxError: 'break' outside loop".

Why does SyntaxError: 'continue' not properly in loop happen?

1. The continue is indented out of the loop

In Python, indentation decides what is inside the loop. One level too little and the line is no longer part of the loop body.

Breaks

for n in [1, -2, 3]:
    if n < 0:
        print("skip")
continue

Works

for n in [1, -2, 3]:
    if n < 0:
        print("skip")
        continue
    print(n)

2. The continue is inside a helper function

A function body is its own block. Even if the function is called from inside a loop, a continue inside the function is not inside any loop.

Breaks

def check(n):
    if n < 0:
        continue
    print(n)

for n in [1, -2, 3]:
    check(n)

Works

def check(n):
    if n < 0:
        return   # leave the function instead
    print(n)

for n in [1, -2, 3]:
    check(n)

3. Using continue to skip code that is not in a loop

Outside a loop there is nothing to continue to. Use an if/else, or return from a function, to skip code.

Breaks

name = ""
if not name:
    continue
print("Hello", name)

Works

name = ""
if name:
    print("Hello", name)

How to find it in your own code

Look at the line Python points to and follow the indentation upwards. Is there a for or while line above it, at a smaller indent, with no def in between? If not, the continue is outside the loop. Move it into the loop body, or replace it with return (inside a function) or an if/else (anywhere else).

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides