-1

if (int4/int3) == (int3/int2) == (int2/int1), I will do something. How do I handle division error if I wanna take in results of int4==int3==int2==int1==0 also

 try:
     (int4/int3) == (int3/int2) == (int2/int1)
 except ZeroDivisionError:
      PerformA()
 else:
      PerformA()
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Gavin
  • 2,784
  • 6
  • 41
  • 78
  • how about "if int2 != 0 and (int1/int2) == (int3/int2):" ... – Pascal Sep 11 '14 at 13:35
  • 6
    Not relevant to your question, but since this is Python, your indentation matters. Do fix it. – Bucket Sep 11 '14 at 13:36
  • 2
    Not clear what you mean by your empty `if`-block, it's not valid Python. – bereal Sep 11 '14 at 13:38
  • Why are you using division to test if values are zero? `if int1 or int2 or int3: doSomethingWithNonZeroValues() else: doSomethingWithAllZeroValues()` – Henry Keiter Sep 11 '14 at 13:41
  • Are you only trying to check if all your ints are 0? Because that is a totally different question and can be done with ```all(map(lambda x: x == 0, [int1, int2, int3, int4]))``` (or lots of other ways) – wnnmaw Sep 11 '14 at 13:41
  • Yes, what you're doing is saying `if = = then do something` but you never actually say what that something is. This means that if that statement is true, your script will freeze up and do nothing. You also have your `else` in your except block, not after your `if` which means that if the `if` statement is not true, then your script doesn't know what to do and will either freeze up or crash. So far you've limited yourself to `if statement is true, do nothing, if there's an error do A` nothing to say what to do if statement is false. – Joe Smart Sep 11 '14 at 13:42
  • 1
    @Joe that's a [`try-except-else` clause](https://docs.python.org/2/tutorial/errors.html#handling-exceptions). Whether that's what the asker intended, I'm not sure. – Henry Keiter Sep 11 '14 at 13:45
  • Sorry guys. What I mean is if (int4/int3) == (int3/int2) == (int2/int1), I will do something. How do I handle division error if I wanna take in results of int4==int3==int2==int1==0 also – Gavin Sep 11 '14 at 13:45
  • What if, for example, only `int1==0`? – bereal Sep 11 '14 at 13:47
  • @HenryKeiter Learn something new every day! – Joe Smart Sep 11 '14 at 13:55
  • Note that you have a logical error in your code now: it doesn't differentiate inputs `(0, 1, 1, 1)` and `(0, 0, 0, 0)` and presumably you want to. – Dima Tisnek Sep 11 '14 at 15:23
  • What's actually wrong with your existing code? You ask for "more efficient way"… Do you think `try`/`except` is slow? Is this even remotely a bottleneck in your code in the first place? – abarnert Sep 12 '14 at 07:08

2 Answers2

2

If I understand your question correctly, you just need to know how to best handle this comparison when some of the comparisons could be invalid. The pattern I normally see (and use) is to check for edge conditions (if they could be quite common) and handle them. If no edge conditions exist, you can move on to the standard process.

In this particular instance, you have one edge case defined: when one of your denominators is zero. So check whether any of int1, int2, or int3 is zero, and do something appropriate if so. If none of them are, you can continue with your ratio comparison.

if not (int1 and int2 and int3):
    do_something_with_zero_values()
elif (int4/int3) == (int3/int2) == (int2/int1): # This is now a safe operation
    do_something()

Of course, you could also use exception handling to deal with your edge cases. Prescriptively, you generally want to use exceptions only in "exceptional" circumstances, so if zero really is a legitimate value for these variables, I would recommend against conceptualizing it as an exception. See this question for more on that discussion.

Community
  • 1
  • 1
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • I prefer to use `all` for stuff like this: `if not all([int1, int2, int3])`, especially if it's a long list or it's already a list. – mtik00 Sep 11 '14 at 14:02
  • @mtik00 Sure, but these variables (as far as I can tell) aren't in a list already, and there are only three of them. I normally save `all` (or `any`) for when I'm doing something more sophisticated than direct truthiness; e.g. `if any(math.isnan(n) for n in big_list_of_floats):` – Henry Keiter Sep 11 '14 at 14:06
0

Depending on what you actually want to achieve (0 semantics are not clear), here are two options:

if 0 in (int1, int2, int3, int4) or int1/int2 == int2/int3 == int3/int4:
    foo()

or

if not any(int1, int2, int3, int4) or int1/int2 == int2/int3 == int3/int4:
    foo()
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120