Why "if-else-break" breaks in python?

33,065

Solution 1

If I run this, I get the following error:

...     print(a) if a > 0 else break
  File "<stdin>", line 2
    print(a) if a > 0 else break
                               ^
SyntaxError: invalid syntax

This is because

print(a) if a > 5 else break

is a ternary operator. Ternary operators are no if statements. These work with syntax:

<expr1> if <expr2> else <expr3>

It is equivalent to a "virtual function":

def f():
    if <expr2>:
        return <expr1>
     else:
         return <expr3>

So that means the part next to the else should be an expression. break is not an expression, it is a statement. So Python does not expect that. You can not return a break.

In , print was not a function either. So this would error with the print statement. In print was a keyword.

You can rewrite your code to:

a = 5
while True:
    if a > 5:
        print(a)
    else:
        break
    a -= 1

You can read more about this in the documentation and PEP-308.

Solution 2

If is an expression, break similar to return is a statement. You can't use two statements in a single sentence (unless you use a semicolon which is ugly). I know it would have been really cool if we can do that, but alas that's the way it is.

Solution 3

To put it in slightly simpler terms, you're misusing the 'one-line if statement' (ternary operator). It always evaluates to an expression (i.e., a value). That is,

<expr1> if <condition> else <expr2>

evaluates to <expr1> if <condition> is True, and to <expr2> if <condition> is False. This resulting value can then be used like any Python value, for example:

y = 0
x = (5 if (y > 0) else 6)
print(x) # 6

Of course, the parentheses are completely unnecessary (even discouraged), but hopefully are useful for understanding the meaning of that line.

Therefore,

print(a) if a > 0 else break

tries to evaluate print(a) (which, by the definition of print() in Python 3, always returns None – perfectly valid, but probably not what you usually want) and then break, which does not evaluate to anything because it is a statement (action), not an expression (value), hence the invalid syntax error.

Hence, if you want to execute one of two statements depending on a condition, you really need the multi-line solution proposed by Willem Van Onsem. There may be hacky ways to do it in one line, but multiple lines is the usual solution for something like this in Python.

Share:
33,065
Manish Goel
Author by

Manish Goel

Updated on September 13, 2021

Comments

  • Manish Goel
    Manish Goel over 2 years

    I am trying to use if-else expression which is supposed to break the loop if the if condition fails, but getting an invalid syntax error.

    Sample code:

    a = 5
    while True:
        print(a) if a > 0 else break
        a-=1
    

    Of course, if I write in the traditional way (not using the one liner) it works.

    What is wrong in using the break command after the else keyword?

  • Davy M
    Davy M almost 7 years
    The answer by Willem Van Onsem has a lot of useful information, but I like the clarity and ease of this answer for practical use; it's easier to remember simply that you can't use two statements in a single sentence.