The continue keyword moves the order of a program onto the next iteration in a loop. If you use a continue statement outside of a for loop or a while loop, the
SyntaxError: continue not properly in loop error will be raised.
This guide explores what this error means and why you may encounter it. It walks you through an example of this error so you can figure out how it works.
SyntaxError: continue not properly in loop
You can only use a continue statement in a loop. This is because continue statements are designed to appear in loops. You cannot use a continue statement to instruct a program to
continue outside of a loop because there is no context for the keyword to interpret what needs to continue.
Continue statements can appear inside an if statement or another block of code, as long as that block of code is inside a loop.
An Example Scenario
Let’s build a program that validates the input for an inventory management system at a coffee house. For an input to be valid, it must contain at least three characters.
To start, let’s ask the user to insert the name of a product that they want to enter into the system:
product_name = input(“Enter the product name: ”)
Next, let’s validate this response. We’ll use an if statement to make sure that the input is at least three characters long:
if len(product_name) < 3: print("Product names must be at least three characters long.") continue else: break print("Your product name is valid.")
If a user inserts a product name under three characters long, a message is printed to the console and then a continue statement runs. Otherwise, a break statement runs. We use the len() method to retrieve the length of the product name.
If our product name is valid, a message informing us this is the case is displayed on the console.
Now that we’ve written our validator, we are ready to run our code. Let’s see what happens when we run our program:
File "main.py", line 3 continue ^ SyntaxError: 'continue' not properly in loop
Our code returns an error.
We’ve used continue statements to tell our program to keep going if a particular condition is met. While we can use a continue statement in an if statement, our continue statement must appear somewhere within a loop.
We do not use a loop in our program which makes our use of continue somewhat counterproductive. What’s more is that we are using continue, which causes the error. To fix this error, we need to enclose our code in a loop:
while True: product_name = input("Enter the product name: ") if len(product_name) < 3: print("Product names must be at least three characters long.") continue else: break print("Your product name is valid.")
We have made one change to our code. All of our program is now inside a while loop. This means that our user will be prompted to enter a product name until the loop stops.
Our loop only stops if the user inserts a valid product name. Otherwise, a message is printed to the console and our loop will iterate again.
Let’s run our program and see what happens:
Your product name is valid.
Enter the product name: Rwandan
Our code runs successfully! Let’s try to run our code on an invalid product name:
Enter the product name: RW
Product names must be at least three characters long.
Enter the product name:
Our program informs us that the product name is invalid and prompts us to insert another product name.
SyntaxError: continue not properly in loop error is raised when you try to use a continue statement outside of a for loop or a while loop. To fix this error, enclose any continue statements in your code inside a loop.
Now you have the knowledge you need to fix this error like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more