Syntax error 'continue' not properly in a loop [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
elif command == 'join':
if len(params) < 1: continue
safeexec(params[0], getattr(botimpl, 'onenter', None), (params[0], prefix))
it says
Syntax error 'continue' not properly in a loop
when I run the file.

You cannot continue from an if statement. You need it to be in a loop.
for x in range(10):
if x == 4:
continue
# Do work
Whereas,
if x == 4:
continue
is wrong.
Python Docs state this:
The continue statement, also borrowed from C, continues with the next iteration of the loop:

The continue keyword is used to go directly to the next iteration of a loop, skipping the code after the keyword. So, the error
SyntaxError: 'continue' not properly in loop
indicates that the elif clause is not inside a loop (like for or while).
What can you do?
Check your logic. Do you really want to use continue?
Put the code inside a loop:
while (...):
if command == 'join':
continue

continue must be inside a while or for loop, check if your elif statement is within a for or while loop. if yes, check your indentation.

The continue keyword is reserved to skip the iteration of a loop and not a if condition block. What you want to do is more this:
elif command == 'join' and len(params) >= 1:
safeexec(params[0], getattr(botimpl, 'onenter', None), (params[0], prefix))
From the documentation:
continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or finally clause within that loop. It continues with the next cycle of the nearest enclosing loop.
When continue passes control out of a try statement with a finally clause, that finally clause is executed before really starting the next loop cycle.

Related

I have an indentation error in python 3 in an elif statement how do I fix this [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I keep getting an indentation error on line 17 but I cannot figure out why, this is a code from a tutorial I found online (https://picovoice.ai/docs/quick-start/porcupine-python/)
this is the error message:
File "/home/luke/Documents/wake_word/wake_word.py", line 17
elif keyword_index == 1:
^
Indentation Error: expected an indented block
I am using vim text editor and python3, here's my code:
import pvporcupine
porcupine = pvporcupine.create(
access_key='${ACCESS_KEY}',
keywords=['picovoice', 'bumblebee']
)
def get_next_audio_frame():
pass
while True:
audio_frame = get_next_audio_frame()
keyword_index = porcupine.process(audio_frame)
if keyword_index == 0:
# detected `porcupine`
elif keyword_index == 1:
# detected `bumblebee`
Comments do not count as a statement. To add a null statement, put pass keyword below the if/elif/else condition to avoid errors.
Also, an else statement after would help eliminate any future problems.
Eg:
if keyword_index == 0:
pass
# detected `porcupine`
elif keyword_index == 1:
pass
# detected `bumblebee`
Comments are not taken in account by the interpreter. So it sees:
if keyword_index == 0:
elif keyboard_index == 1:
and this is an illegal code because there's no things to execute after the :.
I suggest adding the keyword pass after the : llike this:
if keyword_index == 0:
# detected `porcupine`
pass
elif keyboard_index == 1:
# detected `bumblebee`
pass
so the interpreter will know there's nothing to do and will not say, "I see nothing to execute so let's raise an error".

Why does python 3 forbid if-condition after while-loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I've got indentation error in the following code, python 3.9
while list1:
# do smth
if condition: # do smth
The error is fixed if I put smth after while loop. It seems, that python 3 forbids if condition after while-loop. Is it a bug or a feature?
while list1:
# do smth
var1 = 0
if condition: # do smth
# comments are ignored by the interpreter. In order for the block to be valid it must have at least one statement. This is what pass is typically used for:
while list1:
# do smth
pass
if condition:
# do smth
pass
Python expects an indented block after a while loop. A commented out section of code does not count. So if you first have this:
while n < 10:
n += 1
and then (e.g. for debugging purposes) comment out the loop body:
while n < 10:
# n += 1
Python will complain:
^
IndentationError: expected an indented block
If you really wish to comment out the whole loop body of a while loop, temporarily add a dummy pass statement:
while n < 10:
# n += 1
pass

Why is my if statement printing my else without error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have tried everything I could before asking this question... I am making a little text adventure game, really simple and the only code in it is basically just input(). But when they do something wrong, I want to add an else: and print("Thats not valid!") and go back to example = input(), but my else statement is printing and re-doing input with a correct statement, so its still executing else without any error, can anyone help? I don't know why its doing this and all the examples I have seen this is valid code, and my else statement just executes without checking if it was wrong...
```python
if prompt1 == "y":
print("Lets start")
else:
prompt1 = input()
It then executes input again...
If that's the exact code the indention is wrong.
The if and else of same condition should have same indention:
if condition:
do this
else:
do this

How can I define a Boolean to a variable? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to make a calculator. I have the code that does the operations working, but I am trying to allow the user to keep the calculator running without having to rerun the code. i an trying to assign a Boolean to variable, but python keeps telling me there is a name error, which is the the variable is not defined. Can you please help me?
Thank you in advance.
I have tried to change the available name but it doesn't do anything.
The code stopped working when it got to the while run == true line.
else:
print('Invalid operator, please run code again')
run = True
while run == True:
print(' do you need another problem solved? y/n')
if input() == y:
run = True
elif input() == n:
run = False
I expected the code to ask me if I need another problem solved, but there is a name error.
If the error is complaining about y or n it's because you need to surround it with quotes
if input() == "y":
run = True
Also, run == True is not needed
while run:
does the trick
else:
print('Invalid operator, please run code again')
run = True
while run:
print(' do you need another problem solved? y/n')
inp=input()
if inp == 'y':
run = True
elif inp == 'n':
run = False
you have used input 2 times so your code will wait 2 times for input, use input() once.

Python: while loop that quits program if not completed within specified limit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Python Question: i need to run a program that asks for a password but if the wrong answer is input three times the user is thrown out of the program i can run it in a while loop but cant get it to quit if the wrong password is entered.
Thanks for your help
Adding an approximation of how I'd do it, in the absence of an example containing the problem. else on a for loop will only execute if you did not break out of the loop. Since you know the max number of times to run the loop is 3 you can just use a for loop instead of a while loop. break will still break you out early.
for _ in range(3):
if raw_input("Password:") == valid_passwd: # really should compare hashed values (as I shouldnt have passwords stored in the clear
print "you guessed correctly"
break
print "you guessed poorly"
else:
print "you have failed too many times, goodbye"
sys.exit(1)
# continue on your merry (they got the right password)
How about sys.exit()
>>> import sys
>>> guess = False
>>> if guess:
... pass
... else:
... sys.exit()
http://docs.python.org/3/library/sys.html

Categories