1

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.

5
  • The pass statement is only a place holder, and it doesn't do anything. Commented Feb 10, 2015 at 22:35
  • 5
    How can x equal to both 2 and 3 at the same time? If you want your condition to be checked every time, change it from elif to if Commented Feb 10, 2015 at 22:35
  • 1
    what do you mean by "pass it back out" ? if you have entered an if/else branch you cannot enter any others Commented Feb 10, 2015 at 22:36
  • yes, @MarkusMeskanen you are correct. How can i exit that elif x == 2 block if it doesnt satisfy it and check remaining elif, else? Commented Feb 10, 2015 at 22:37
  • can you please post your actual code and conditions for the ifs/elses? Commented Feb 10, 2015 at 23:01

3 Answers 3

3
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

Sign up to request clarification or add additional context in comments.

2 Comments

what would that even do?
Continue on to the next clause as if the preceding clause wasn't a match... lol
0

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:

6 Comments

Though, you don't need to change all of the elif statements, just the final one.
see my comment on @JonathanEpstein's solution
@JoranBeasley, saw your "what if" comment. There are a lot of "what ifs" that could be coded here, hard to know how to answer that. My answer is just one way to solve the OP's situation.
really there are only 2 what if that need to be considered with this approach... and you clearly considered one of them (mutually exclusive conditions) ... that said i dont know who the mystery downvoter is (I am assuming this is a toy example since OP's question cannot exist where x is equal to both 2 and 3 ...)
This was really a syntax question. The statements were not supposed to represent a real application. I just wanted to know how to get out of that inner block of if statements and check the next elif. I didnt realize that it had to be a new If statement.
|
0

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

else:
    pass it back out
if x  == 3

4 Comments

what if one of the branches changes x? ... (I didnt downvote but just something to consider)
@Jonathan Epstein Thank you. I didnt realize that I have to start a new If block after that. This works great.
@JoranBeasley -- a branch changing the value of x isn't something the poster had indicated. Though if he does want to do that, it's just as possible he wants this function to reflect x's new value, as opposed to its original value.
I assume your right .... but do something is pretty vague ... and the condition that he claims to want to check is impossible to achieve ... so tbh I wouldnt necessarilly bet the farm on much with this question ... all I was pointing out was that this can have significantly different effects than his original code (assuming his conditions are not his real conditions and do something is not his real code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.