Python continue outside loop

How to fix «SyntaxError: ‘continue’ not properly in loop» in Python

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/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. 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 

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 

Источник

Python continue Statement

Continue Statement Flow Diagram

We can’t use any option, label or condition with the continue statement.

Python continue Statement Examples

Let’s look at some examples of using the continue statement in Python.

1. continue with for loop

Let’s say we have a sequence of integers. We have to skip processing if the value is 3. We can implement this scenario using for loop and continue statement.

t_ints = (1, 2, 3, 4, 5) for i in t_ints: if i == 3: continue print(f'Processing integer ') print("Done")

Python Continue Statement For Loop

2. continue statement with the while loop

Here is a simple example of using continue statement with the while loop.

count = 10 while count > 0: if count % 3 == 0: count -= 1 continue print(f'Processing Number ') count -= 1

Python Continue Statement While Loop

3. continue statement with a nested loop

Let’s say we have a list of tuples to process. The tuple contains integers. The processing should be skipped for below conditions.

  • skip the processing of tuple if its size is greater than 2.
  • skip the execution if the integer is 3.

We can implement this logic with nested for loops. We will have to use two continue statements for implementing above conditions.

list_of_tuples = [(1, 2), (3, 4), (5, 6, 7)] for t in list_of_tuples: # don't process tuple with more than 2 elements if len(t) > 2: continue for i in t: # don't process if the tuple element value is 3 if i == 3: continue print(f'Processing ')

Continue Statement With Nested Loop

Why Python doesn’t support labeled continue statement?

Many popular programming languages support a labeled continue statement. It’s mostly used to skip the iteration of the outer loop in case of nested loops. However, Python doesn’t support labeled continue statement.

PEP 3136 was raised to add label support to continue statement. But, it was rejected because it’s a very rare scenario and it will add unnecessary complexity to the language. We can always write the condition in the outer loop to skip the current execution.

Источник

Читайте также:  Найти целую часть от числа питон
Оцените статью