Reputation: 45
I try to understand why I get unreasonable result from the following if
:
def print_if_neg (a,b):
if a < 0 != b < 0:
print "Only One Neg"
else:
print "0 or 2"
print_if_neg(1,1)
print_if_neg(-1,1)
print_if_neg (1,-1)
print_if_neg(-1,-1)
I get 3 times 0 or 2
and then last one Only One Neg
.
What is the order of this complicated condition?
I've tried this:
if (a < 0) != (b < 0):
and it's ok but I'm trying to understand why above doesn't work.
Upvotes: 1
Views: 50
Reputation: 1757
From this, you could make it more readable imo:
from operator import xor
def print_if_neg (a, b):
if xor(a < 0, b < 0):
print "Only One Neg"
else:
print "0 or 2"
Upvotes: 0
Reputation: 25
Due to operator precedence you need to place the two conditions in parentheses for your expected results. Otherwise the comparison operators are solved, checking for 0 != b
in your code, which is not what you expect.
def print_if_neg (a,b):
if (a < 0) != (b < 0):
print ("Only One Neg")
else:
print ("0 or 2")
print_if_neg(1,1)
print_if_neg(-1,1)
print_if_neg (1,-1)
print_if_neg(-1,-1)
Note that all comparison operators have the same precedence and comparisons can be chained arbitrarily, e.g., x < y <= z
is equivalent to x < y
AND y <= z
Upvotes: 1
Reputation: 8378
This is because condition a < 0 != b < 0
means a < 0
AND 0 != b
AND b < 0
First of all when a >= 0
first condition evaluates to False and so nothing else gets evaluated. Then, if a is <0 but b=1 last condition in the chain is False. Therefore your chained condition is False 3 out of 4 times.
This is well explained in section 6.10 of Python documentation.
Upvotes: 0
Reputation: 36823
As CoryKramer pointed out, the operator precedence is making the difference.
Your code is equivalent to this:
def print_if_neg (a,b):
if a < (0 != b) < 0:
print "Only One Neg"
else:
print "0 or 2"
Because !=
has higher precedence than <
by language definition.
So, use ()
to force the precedence that you need:
def print_if_neg (a,b):
if (a < 0) != (b < 0):
print "Only One Neg"
else:
print "0 or 2"
Also, FYI you are coding the xor
operator.
Upvotes: 1
Reputation: 117926
You need parentheses due to operator precedence
def print_if_neg (a,b):
if (a < 0) != (b < 0):
print "Only One Neg"
else:
print "0 or 2"
Upvotes: 1