The syntaxerror: ‘continue’ not properly in loop is a common error message usually encounter while using loop structure in your Python code.
This error is not hard to fix as you think, but in order to resolve this error.
We need a proper understanding of how to fix the syntaxerror continue not properly in loop error.
Fortunately, this article will walk you through troubleshooting this error, so let’s get started!
What is “syntaxerror continue not properly in loop”?
The syntaxerror: ‘continue’ not properly in loop occurs when you’re trying to use the continue statement outside of a loop.
It usually happens when the Python interpreter encounters the continue statement outside the loop.
For example:
def sample():
a = 10
if a == 10:
continue
sample()
When we try to run this sample code, it will result in a SyntaxError.
File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 4
continue
^^^^^^^^
SyntaxError: 'continue' not properly in loop
Note: Continue and break works with for loop and while loop. The break statement terminates the loop. Meanwhile, the continue statement skips one iteration.
What is continue statement in Python?
The continue statement is used in loops to skip the rest of the code inside a loop for the current iteration and move to the next iteration of the loop.
Unfortunately, if it is not placed within a loop or is misaligned, the interpreter throws SyntaxError.
Why does “continue not properly in loop” error occurs in Python?
This error can occur due to some reason, such as:
👎 Incorrect usage of the continue statement is one of the major causes of why this error keeps on bothering you. The continue statement can only be used, within the body of a loop, such as a for or while loop.
👎 When the continue statement is placed outside of a loop or within a different block of code, this error will occur.
👎 Another thing is when you used the continue statement in the if block, that’s not part of a loop.
How to fix the “syntaxerror: ‘continue’ not properly in loop”?
To fix the syntaxerror continue not properly in loop error, ensure that the continue statement is used within a loop (for, while). Also, check the indentions of your code.
Here are the following solution, which you can vary pin order to resolve this error.
Solution 1: Put the continue statement inside the Loop
By making sure that the continue statement is placed within the loop structure, we can fix the error.
Incorrect code:
def sample():
a = 10
if a == 10:
continue
sample()
Corrected code:
✅ def sample():
for a in range(10):
if a == 10:
continue
print(a)
sample()
As you can see, the continue statement is used inside a for loop. When the value of a is equal to 10, the continue statement is executed and the rest of the code inside the loop is skipped for that iteration.
This ensures that the continue statement is used properly within the loop, eliminating the syntax error.
Output:
0
1
2
3
4
5
6
7
8
9
Here’s an example code using while loop:
✅ def sample():
a = 0
while a < 10:
if a == 10:
a += 1
continue
print(a)
a += 1
sample()
Output:
0
1
2
3
4
5
6
7
8
9
Solution 2: Remove the continue statement and replace it with an if statement
For example:
✅ def sample():
for a in range(3):
if a != 3:
print(a)
sample()
Rather than using a continue statement to skip printing the value of a when it is equal to 3, we use an if statement to only print the value of a when it is not equal to 3.
Output:
0
1
2
Solution 3: Remove the continue statement and replace iit with a pass statement
You can fix the error by replacing the continue statement with a pass statement. The pass statement does nothing and is used as a placeholder when you need a statement in your code.
For example:
✅ def sample():
for a in range(8):
if a == 5:
pass
else:
print(a)
sample()
In this example, the value of a is equal to 8, the pass statement is executed and no error appears.
Output:
0
1
2
3
4
6
7
Solution 5: Use proper indention
Ensure your code in the loop has been indented appropriately.
Incorrect code:
for a in range(15):
if a % 3 == 0:
continue
print(a)
Python interpreter can’t read your code if it is indented incorrectly.
You can use the tab to correct the indention of your code.
Corrected code:
✅ for a in range(15):
if a % 3 == 0:
continue
print(a)
Output:
1
2
4
5
7
8
10
11
13
14
Conclusion
In conclusion, the error message syntaxerror: ‘continue’ not properly in loop occurs when you’re trying to use the continue statement outside of a loop.
It usually happens when the Python interpreter encounters the continue statement outside the loop.
To fix the syntaxerror continue not properly in loop error, ensure that the continue statement is used within a loop (for, while). Also, check the indentions of your code.
This error message is easy to fix and understand, by executing the solutions above, you can master this SyntaxError with the help of this guide.
You could also check out other SyntaxError articles that may help you in the future if you encounter them.
- Syntaxerror unexpected identifier
- Syntaxerror invalid character in identifier
- Syntaxerror: invalid decimal literal
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊