I have a for loop and within that there is a simple single line if condition. I want to use continue option in the else part.
This does not work :
def defA() :
return "yes"
flag = False
for x in range(4) :
value = defA() if flag else continue
^
SyntaxError: invalid syntax
Working code :
for x in range(4) :
if flag :
defA()
else :
continue
X if C else Y
is an expression.continue
is a statements. Expressions in Python cannot contain statements. Transform yourif
expression into anif
statement to fix. – Smorgasbordcontinue
). – Standoff