Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elif-row without else python

Is it possible to write an else at the end of an if-row which only gets executed if none of all the if statements are true? Example:

if foo==5:
    pass
if bar==5:
    pass
if foobar==5:
    pass
else:
    pass

In this example, the else part gets executed if foobar isn't 5, but I want it to be executed if foo, bar and foobar aren't 5. (But, if all statements are true, all of them have to be executed.)

like image 448
Nearoo Avatar asked Sep 20 '25 14:09

Nearoo


1 Answers

How about doing something like this? Making four if statements but the fourth if statement won't be run if one of the other three is run because the other statements change the variable key

key = True

if foo == 5:
    key = False
if bar == 5:
    key = False
if foobar == 5:
    key = False
if key:
    pass # this would then be your else statement
like image 118
Maggick Avatar answered Sep 22 '25 03:09

Maggick