Last updated: Apr 8, 2024
Reading timeยท3 min
The Python "SyntaxError: 'continue' not properly in loop" occurs when we use
the continue
statement outside of a loop.
To solve the error, use the continue
statement in a for
or a while
loop
and make sure your code is indented correctly.
continue
statement outside a loopHere is an example of how the error occurs.
if len('hi') == 2: # โ๏ธ SyntaxError: 'continue' not properly in loop continue
Using the continue
statement outside a
for or a while loop causes the error.
The continue
statement can be used to continue to the next iteration of a
for
or a while
loop.
for i in range(10): if i % 2 == 0: continue print(i) # ๐๏ธ 1, 3, 5, 7, 9
The example above checks if each number in the range is divisible by 2 and if it is, we continue (skip) to the next iteration.
continue
statement in a while
loopHere is an example that uses a continue
statement in a while
loop.
num = 0 while True: num += 1 if num > 10: break if num % 2 == 0: continue print(num) # ๐๏ธ 1, 3, 5, 7, 9
The while
loop iterates until its condition returns false or the break
statement is used.
The break statement breaks out of the
innermost enclosing for
or while
loop.
Whereas, the continue statement continues to the next iteration of the loop.
Make sure you have indented your code in the loop correctly.
for i in range(10): if i % 2 == 0: # โ๏ธ Bad indentation continue print(i) # ๐๏ธ 1, 3, 5, 7, 9
For the Python interpreter to be able to read your code, it has to be indented correctly.
Use tabs to indent the code as shown in the following code sample.
for i in range(10): if i % 2 == 0: # โ Proper indentation continue print(i) # ๐๏ธ 1, 3, 5, 7, 9
continue
statement is not indented consistently, the interpreter won't recognize it as part of the loop.You should either indent your code using tabs or using spaces.
However, you should never mix the 2 because mixing tabs and spaces in the same code block often causes issues in Python.
pass
statement to implement the block laterIf you need to handle a block of code that's not yet implemented, use the pass
statement.
if len('hi') == 2: pass
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
return
statement to return a value from a functionIf you meant to return a value from a function, use the return statement.
def get_name(): return 'Bobby Hadz' name = get_name() print(name) # ๐๏ธ 'Bobby Hadz'
The return
statement can be used to return a value and exit the function.
If you don't need to return a specific value and just need to exit the function,
simply return
.
The continue statement can only be used in a for or a while loop.
The statement cannot be used in a function or a class that is within a loop.
The continue
statement continues to the next iteration of the enclosing loop.
for i in range(10): if i % 2 == 0: continue print(i) # ๐๏ธ 1, 3, 5, 7, 9
On each iteration where the i
variable stores a number that is divisible by
2
, a continue
statement is used to skip to the next iteration of the loop.
You can learn more about the related topics by checking out the following tutorials: