Python Continue Not Properly in Loop
While writing code in Python, we need to follow some rules, that define how the code must be written, which is also known as syntax. Every individual statement has its own syntax. And if we do not follow that syntax, Python raises the SyntaxError Exception.
The continue is a Python keyword and a loop control statement. It can only be written inside the loop body, and if we try to use it outside the loop, there Python will raise the SyntaxError: 'continue' not properly in loop
error.
This Python guide discusses the following error in detail and demonstrates a common example scenario with a solution. By the end of this article, you will have a complete idea of what is SyntaxError: 'continue' not properly in loop
Error in Python, why it occurs, and how to debug it.
Python Problem: SyntaxError: 'continue' not properly in loop
This error raises in a Python program when the continue
statement is written outside the for or while loop body.
Example
age = 20 if age>=18: print("You are eligible to get the vaccine ") continue else: print("You are not Eligible for vaccing")
output
File "main.py", line 4 continue ^ SyntaxError: 'continue' not properly in loop
The error statement has two sub statements separated with a colon :
.
- SyntaxError
- 'continue' not properly in the loop
1. SyntaxError
SyntaxError is one of Python's standard exceptions. Python parser raises this exception when it finds some statement is not following the defined syntax.
2. 'continue' not properly in the loop
This is the error message, telling us that the continue
keyword is not inside the loop body. We only receive this error message when we use the continue
keyword outside the loop body. In the above example, we have used the continue
in the if..else
body, that's why Python's parser raised the error.
Common Example Scenario
continue
can only be used within the for
or while
loop body, and it continues to the next iteration of the loop. Inside the loop we can use it anywhere, generally, we put it inside the if..else
condition so it can only execute for specific conditions not for every iteration.
Example
Let's create an input validator that asks the user to enter a valid 4 digit passcode between 1000 to 9999. And if the user enters the valid number we will display a message that "the passcode is valid" if not we will display the message that "the passcode is invalid" and ask the user to enter the passcode again.
passcode = int(input("Enter a valid 4 digit passcode (1000 to 9999): ")) #if passcode is not valid if not (passcode >=1000 and passcode <=9999): print("Your Passcode is Not valid \n Please enter again ") continue else: print("The entered Passcode is valid")
Output
File "main.py", line 6 continue ^ SyntaxError: 'continue' not properly in loop
Break the code
Python is raising the error in the above example because the continue
statement is not inside any loop statement. The logic we have put in the above example misses the loop statement.
Solution
To solve the above problem we need to put all the code inside the while loop statement with the default condition True, which will make the loop infinite. And the user can only exit that loop when it enters the valid passcode.
while True: passcode = int(input("Enter a valid 4 digit passcode (1000 to 9999): ")) #if passcode is not valid if not (passcode >=1000 and passcode <=9999): print("Your Passcode is Not valid \nPlease enter again ") continue else: #if the passcode is valid print the statement and get out of the loop print("The entered Passcode is valid") break
Output
Enter a valid 4 digit passcode (1000 to 9999): 99999 Your Passcode is Not valid Please enter again Enter a valid 4 digit passcode (1000 to 9999): 9898 The entered Passcode is vaid
Now the Python script runs without any SyntaxError.
Conclusion
While working with the loops we get two-loop control keywords continue
and break
. These two keywords are exclusive for loop statements (for and while). And if we use these keywords outside the loop code block we receive the Syntax Error with an error message. For the continue
outside the loop scope, Python throws the 'continue' not properly in loop
Error and for break
it throws 'break' outside the loop
error.
This error is very common in Python, and debugging it is also very easy. The only thing you need to keep in mind that, you can not use these two keywords outside the loop scope. If you are still getting this error in your Python program, please share your code in the comment section. We will try to help you in debugging.
People are also reading:
- Python typeerror: 'str' object is not callable Solution
- How to loop with indexes in Python?
- Python NameError name is not defined Solution
- How To Make a Game With Python?
- Python TypeError: 'NoneType' object is not callable Solution
- Flatten List & List of Lists in Python
- Python IndexError: tuple index out of range Solution
- What is Tornado in Python?
- How to find and remove loops in a Linked List?
- Python TypeError: '>' not supported between instances of 'str' and 'int' Solution
Source: https://www.techgeekbuzz.com/blog/python-syntaxerror-continue-not-properly-in-loop-solution/
0 Response to "Python Continue Not Properly in Loop"
Post a Comment