简体   繁体   中英

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.

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

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:

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

else:
    pass it back out
if x  == 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM