1

I know someone asked about an "unless" that works as if not, or a not in, but I'd like to have an statement that introduces an exception to a conditional statement. For example:

if num >= 0 and num <= 99:
    # then do sth (except the following line)
unless num = 10: 
    # in this case do something else

It'd much legible and intuitive than writing:

    if (num >= 0 and num <= 9) or (num >= 11 and num <= 99):
        # then do sth (except the following line)
    elif num = 10: 
        # in this case do something else

The if not statement doesn't do the same...

NOTE: I'm practically a newbie into this so please bear with me

Martin
  • 414
  • 7
  • 21
  • 1
    just add `and num != 10` in the first condition, so it happens if it is between o and 99 and [not equal to 10.](http://stackoverflow.com/questions/11060506/is-there-a-not-equal-operator-in-python) – Tadhg McDonald-Jensen Oct 28 '16 at 19:51
  • 1
    I'd disagree that your preposed `unless` is more intuitive, having a large block of code that will run `if num >= 0 and num <= 99` and then seeing below it that that isn't the whole condition would personally drive me crazy. – Tadhg McDonald-Jensen Oct 28 '16 at 19:52
  • Yes! You're totally right..., != 10 in the first line and then elif, is totally equivalent.. Thanks a lot! – Martin Oct 28 '16 at 19:56

3 Answers3

7

An ordinary if can do this, if you reorder the clauses:

if num == 10:
    # do the num = 10 thing
elif 0 <= num <= 99:
    # do something else
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
user3552819
  • 116
  • 2
  • Oohh yes, of course! A small step for a programmer but a big one for a newbie... Reallly useful, thanks a lot! – Martin Oct 28 '16 at 19:59
  • @Martec: unrelated: if you need to consider several ranges then use half-closed intervals such as: `n in range(100)` (`0 <= n < 100`). See [Why numbering should start at zero](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html) – jfs Oct 29 '16 at 15:49
1

your preposed unless would lead to very non-intuitive code in my opinion, When there is an if statement I expect that statement to happen if the whole condition comes true, if there was a way of changing that so that you needed to find the end of the block to see if there was some separate case would be very frustrating when reading code.

Basically you are interested in running the first one when num != 10 is one of the extra conditions, so just use:

if 0<=num <= 99 and num!=10:
    #statement here
elif num == 10:
    #other statement.

Also notice that 0 <= num <= 99 is essentially equivelent to 0 <= num and num <= 99 but is easier to read :)

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • I see it Tadhg, I understand totally that we want to know at first glance what that is doing. If we had later one (or several!) exceptions, it would not be more intuitive at all... It occurs to me though now that *unless*..., sorry, I mean *if* instead of an if, we had a try then it would be different. But the easy solutions you have proposed as a base minimum are equally efficient, so I don't see the need for unless now. Thanks :) – Martin Oct 28 '16 at 20:11
0

Reverse the else and check for the 10 first.

If num == 10:
    # in this case do something
elif num >= 0 and num <= 99:
    # then do sth
Simon Black
  • 913
  • 8
  • 20