SyntaxError: ‘continue’ not properly in loop in Python (Solved)

Updated Sep 22, 2023 ⤳ 2 min read

Python raises “SyntaxError: ‘continue’ not properly in loop” whenever it encounters a continue statement outside a loop – usually within an if block that’s not part of a loop.

Here’s what the error looks like:


File /dwd/sandbox/Desktop/test.py, line 8
    continue
    ^^^^^^^^
SyntaxError: 'continue' not properly in loop

A continue statement is a control flow feature used within a loop to skip the rest of the current iteration and continue to the beginning of the next one.

The difference between continue and break is that a break statement terminates the loop while continue skips one iteration.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

You usually use continue when you reach a specific value and you want to skip the rest of the iteration and proceed to the next iteration. That's pretty much like the C language.

Based on Python syntax, the continue keyword is only valid inside loops - for and while.

In the following example, we iterate over a list of scores and print those above ⭐ 4.5 (inclusive):


scores = [3.5, 3.8, 4.6, 4.5, 4.9, 3.9, 5, 1.2, 3, 4, 4.6]
top_scores = []

for score in scores:
    if (score <= 4.5):
        continue
    
    top_scores.append(score)

print(top_scores)
# Output: [4.6, 4.9, 5, 4.6]

In the above code, if the score value in the current iteration is less than 4.5, we continue to the next iteration.

How to fix SyntaxError: 'continue' not properly in loop

One of the most common causes of "SyntaxError: 'continue' not properly in loop" is using the continue keyword in an if block that's not part of a loop:


user = {'id': 2, 'is_active': True}

if user:
    if is_active != True:
        continue # 🚫 SyntaxError: 'continue' not properly in loop

There's no point in using a continue statement within an if block. If the condition isn't met, the code isn't executed anyway. The above code would only make sense if it's inside a loop:


users = [
    {'id': 1, 'is_active': True},
    {'id': 2, 'is_active': False},
    {'id': 3, 'is_active': True},
]

for user in users:
    if user['is_active'] == False:
        continue
    
    print(f'Sending an email to user {user[id]}')
    # Some code here ...

# Output: 
# Sending an email to 1
# Sending an email to 3

If you want to keep the if block for syntactical reasons, you can replace the continue keyword with the pass keyword.

pass statement does nothing in Python. However, you can always use it when a statement is required syntactically, but no action is needed.

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version