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

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

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

Not instantly getting out a loop with break [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
When I run this easy program and want to exit it, I need to type exit 2 times (at line 7).
def registration():
def username_registration():
entering_username = True
while entering_username:
print("Type exit to leave the registration.")
usernam_from_registration = input("Enter username: ")
if usernam_from_registration == "exit":
break
lenght_usernam_from_registration = len(usernam_from_registration)
if lenght_usernam_from_registration > 15:
print("too long")
else:
return usernam_from_registration
username_registration()
print(username_registration())
registration()
Why is this and how can I make it so I only need to write it one time?
This is because you are calling the username_registration() function twice.
username_registration()
print(username_registration())
The first time you call it, nothing happens because you are not doing anything with the result.

Inline function hack just to use inline if else [duplicate]

This question already has answers here:
Why doesn't exec("break") work inside a while loop
(6 answers)
Closed 3 years ago.
A recent question made me think if it is possible to do something like:
def defA(flag) :
return "value = 'yes'" if flag else "continue"
flag = False
#n can be a reasonable number like 100
for x in range(n):
#some logic that may change the flag
exec(defA(flag))
here, you got a variable assignment if the flag is true, or continue the for loop otherwise, but it gives me an odd error:
SyntaxError: 'continue' not properly in loop
Is it possible or should I let it go?
Because exec doesn't carry the context over to the statement being executed.
pass can be used anywhere, so the context doesn't matter. continue can only be used in the context of a loop, but that context is not available to exec.
You can only use continue in an exec statement if the loop itself is also part of the executed code:
f = 'for x in range(n): if(flag) continue' exec f
In other words, you can only use exec for complete statements, where a single continue (or break) is not complete.

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

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.

Categories