1

I would like to know if there is any way to have one else statement for multiple levels of if statements.

I'll elaborate:

if <condition-1>:
    if <condition-2>:
        do stuff
    elif <condition-3>:
        do other stuff
else: #if either condition-1 or all nested conditions are not met
    do some other thing

I know this could easily be solved by adding a function with the "do some other thing" and making a call to it with both a nested else and a toplevel else but I wanted to know if there was some way to make this look a little bit cleaner.

Thanks in advance, any ideas are welcome.

Dude Random21
  • 55
  • 1
  • 5
  • 1
    here for similar question: http://stackoverflow.com/questions/2069662/how-to-exit-an-if-clause ; tldr; wrap in in its own funciton – jae555 Jan 28 '16 at 05:03

2 Answers2

2

No, not really. That is really the type of thing that python doesn't want you to do. It prefers to keep readability and clarity over "flashy" tricks. You could do that by combining statements or creating a "flag" variable.

For example, you could do

if <condition-1> and <condition-2>:
    # do stuff
elif <condition-1> and <condition-3>:
    # do other stuff
else:
    # do some other thing

Or, if you don't want to keep repeating condition 1 for some reason (it is expensive to check, it is clearer to not keep repeating it, or you just don't want to keep typing it), we could do

triggered_condition = False
if <condition-1>:
    if <condition-2>:
        triggered_condition = True
        # do stuff
    elif <condition-3>:
        triggered_condition = True
        # do some other stuff
if not triggered_condition:
    # do some other thing

If this was used in a function, we could even skip the flag

if <condition-1>:
    if <condition-2>:
        # do stuff and return
    elif <condition-3>:
        # do some other stuff and return
# do some other thing
# if we got here, we know no condition evaluated to true, as the return would have stopped execution
Matthew
  • 7,440
  • 1
  • 24
  • 49
  • Well, that's pretty much what I thought but I figured if you don't ask you'll never know for sure, thanks for the quick reply! – Dude Random21 Jan 28 '16 at 13:55
0

There are a couple of approaches to this that are not particularly intuitive/readable ... but work:

In this one, we exploit the for ... else ... syntax. Any successful condition should issue a break

for _ in [1]:
    if <condition>:
        if <condition>:
            # where ever we consider ourselves "successful", then break
            <do stuff>
            break
        elif <condition>:
            if <condition>:
                <do stuff>
                break
else:
    # we only get here if nothing considered itself successful

Another approach is to use a try ... else ..., where "successful" branches should raise an exception.

These aren't particularly nice, and are not to be recommended!

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
  • I'd advise against using the exception catching approach. That is somewhat of an abuse of it's purpose, and could be very unstable if any of the statements in the nested if's could raise an exception normally. – Matthew Jan 28 '16 at 05:08
  • 1
    @Matthew True ... but if you were nasty enough to use the exception approach, you'd be throwing a custom exception. – donkopotamus Jan 28 '16 at 05:09
  • Thanks for your answer, these methods certainly work but the idea was to improve readability and by removing the redundant else. Very creative however! – Dude Random21 Jan 28 '16 at 14:01