1

Is there a way to execute code in an if/elif structure so it evaluates all the code up to the elif? Here is an example:

i = 1 # this can be anything depending on what to user chooses

x = 5
y = 10

if i == 1:
    z = x + y

elif i == 2:
    # Here I want to return what would have happened if i == 1, in addition other stuff:
    r = x^3 - y

elif i == 3:
    # Again, execute all the stuff that would have happened if i == 1 or == 2, an addition to something new:
    # execute code for i == 1 and i == 2 as well as:
    s = i^3 + y^2

What I'm attemping to do is to avoid explicitly rewriting z = x + y in elif == 2 etc.. because for my application there are hundreds of lines of code to be executed (unlike this trivial example). I guess I could wrap these things in function and call them, but I'm wondering if there is a more concise, pythonic way of doing it.

EDIT: The responses here seem to be focusing on the if/elif part of the code. I think this is my fault as I must not be explaining it clearly. If i == 2, I want to execute all the code for i == 2, in addition to executing the code for i == 1. I understand that I can just put the stuff under i == 1 into the i == 2 conditional, but since it's already there, is there a way to call it without rewriting it?

7
  • 2
    replace elif with if? Commented Aug 1, 2016 at 21:03
  • 1
    @ReutSharabani I deleted my first post but upon thinking it over I'm pretty sure it's not. If i == 2 then the i == 1 block won't get hit.. still not what the OP wants Commented Aug 1, 2016 at 21:06
  • 3
    You are getting some crazy suggestions. Replacing elif with if will not help if the conditions are mutually exclusive. It looks like you would be better served by defining your actual conditions, which is to say not if i < 2 followed by i < 3 followed by i < 4. In other words, it looks like you progressively add more detail as i gets larger. Does this hold? Commented Aug 1, 2016 at 21:06
  • 1
    replace all the == with >=. i.e. use if i >= 1 etc for everything. Commented Aug 1, 2016 at 21:06
  • 1
    If you always want z = x + y to execute, why have it inside an if-statement at all? Maybe I'm not understanding the question. Commented Aug 1, 2016 at 21:07

3 Answers 3

3

Try this:

i = 1 # this can be anything depending on what to user chooses

x = 5
y = 10

if i >= 1:
    z = x + y

if i >= 2:
    # Here I want to return what would have happened if i == 1, in addition other stuff:
    r = x^3 - y

if i == 3:
    # Again, execute all the stuff that would have happened if i == 1 or == 2, an addition to something new:
    # execute code for i == 1 and i == 2 as well as:
    s = i^3 + y^2
Sign up to request clarification or add additional context in comments.

5 Comments

Because no one ever said it was OK to modify i. In particular, it is used under i==3 so you have changed the resulting answer. You broke the OP's code.
@Two-BitAlchemist Sorry. How about this version
It's not necessarily easy to know what OP really wanted, but I feel this answer in its present form either gets him there or gets him close.
This is the direction I wanted to head in. It's difficult to replicate my problem with a trivial example such as this one so I apologize if the question was not clear. But yes, this is very helpful.
@WillVousden Earlier, I had written an answer which changed the value of i by doing i += 1 leading to it being downvoted. Later when I improved my answer, it was approved by people.
1

Perhaps something like this:

if i in (1, 2, 3):
    z = x + y
if i in (2, 3):
    r = x^3 - y
if i == 3:
    s = i^3 + y^2

You can replace the tuples with range(...) when you have many cases.

3 Comments

Am I misunderstanding the question? Why is this answer wrong?
I upvoted you because your answer works. The one I marked correct is more succinct and handles an indefinite amount of cases
@mk8efz Quite right! I was actually referring to the two downvotes on this answer (and the accepted answer also has two downvotes).
1

How an if works is it will execute if the condition passes. Else if is only executed if none of the prior if/elif statements conditions pass. So you want all if statements not elif statements

 i = 1 # this can be anything depending on what to user chooses

x = 5
y = 10

if i >= 1:
    z = x + y

if i >=2:
    # Here I want to return what would have happened if i == 1, in addition other stuff:
    r = x^3 - y

if i >= 3:
    # Again, execute all the stuff that would have happened if i == 1 or == 2, an addition to something new:
    # execute code for i == 1 and i == 2 as well as:
    s = i^3 + y^2

2 Comments

the OP said they wanted z = x + y to also happen in the i==2 case.
you should also change the first block to i >= 1

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.