konrad
konrad

Reputation: 3706

compound if elif else statements + python

I am trying to pass an item out of the compound/nested if/elif/else statement.

if x == 0:
    do something
elif x == 1:
    do something else
elif x == 2:
    if x == 2 and x == whatever:
        do something
    elif x == 2 and x == whatever:
        do something
    else:
        pass it back out
elif x = 3:
    do something
else:
    do something

How can i pass it back out of the inner if so that it gets checked for whether its equal 3?

Does pass statement work here? continue keeps throwing an error.

Upvotes: 1

Views: 537

Answers (3)

Joran Beasley
Joran Beasley

Reputation: 113930

if x == 0:
    do something
elif x == 1:
    do something else
elif x == 2 and y == whatever:
        do something
elif x == 2 and y == whatever:
        do something

elif x = 3:
    do something
else:
    do something

maybe? you cannot enter a new if/elif branch if you have already entered one

another option is to move the rest inside

if x == 0:
    do something
elif x == 1:
    do something else
elif x >= 2:
    if x == 2 and x == whatever:
        do something
    elif x == 2 and x == whatever:
        do something
    elif x = 3:
        do something
    else:
        do something

the other option is to follow the other examples and start a new if block although you need to be a little careful not to check a condition that one of the other branches might change ... and also each option must be mutually exclusive of the other options

Upvotes: 3

Jonathan Epstein
Jonathan Epstein

Reputation: 379

You want to pass it out, but instead of an elif, use an if statement.

else:
    pass it back out
if x  == 3

Upvotes: 0

Brent Washburne
Brent Washburne

Reputation: 13138

You really don't need elif statements since you're checking the value of x in each one. Change them all to if statements, and change the final else statement to if x > 3:

Upvotes: 0

Related Questions