1

I create a function that compare with x and y variable. Inside the function has a lots of nested elif to compare the x and y then return integer. The problem is right now, when it runs at the certain elif statement, it didn't execute the statement although the statement is correct.

def convertTo(self, x, y):
    if( x == 0 & y == 0):
        return 0
    if( x == 0 & y == 1):
        return 1
    if( x == 0 & y == 2):
        return 2
    if( x == 0 & y == 3):
        return 3
    if( x == 1 & y == 0): 
        return 4 # Didn't return this line even though x = 1 and y = 0
    else
        return None

def main():
    self.convertTo(0,0)
    self.convertTo(0,1)
    self.convertTo(0,2)
    self.convertTo(0,3)
    self.convertTo(1,0) # return None? Why?
8
  • 4
    Have you tried and instead of &? Commented Oct 27, 2016 at 13:21
  • 3
    @tobias_k Operator precedence is different. Commented Oct 27, 2016 at 13:23
  • Short circuit? maybe? Just a question... Anyways, how do we know what's returned? The code doesn't output anything Commented Oct 27, 2016 at 13:23
  • 1
    Note that your if/else structure is odd. It should be a series of if/elif/else. But you could also just drop the else: return None entirely, as that's the default anyway. Commented Oct 27, 2016 at 13:23
  • I think the order of operations executes the bitwise operator before the comparision Commented Oct 27, 2016 at 13:24

3 Answers 3

9

You're performing a chained equality comparison which is not doing what you think it does. The bitwise & is performed first as it has a higher priority than ==.

Replace:

x == 1 & y == 0
# 1 == 1 & 0 == 0
# 1 == 0 == 0  False!

With:

x == 1 and y == 0

See: Operator precedence

Sign up to request clarification or add additional context in comments.

2 Comments

This, basically it's parsed as x == (1 & y) == 0.
AKA PEMDAS for adults!
1

In Python, "&" and "and" do two different things. "and" is what you should be using, "&" is a binary operator.

if a = 0011 1100

and

b = 0000 1101

then

a&b = 0000 1100

See http://www.tutorialspoint.com/python/python_basic_operators.htm

2 Comments

But that by itself isn't the entire explanation.
But that does not explain why it fails in this case, after all True & True would still be true. The critical point is indeed operator precedence.
0

You should use and instead of &, as & is a bitwise and.

Chaining multiple conditions in Python is generally done with an if-elif-else statement like below:

if a and b:
   # a and b both was true
elif a and not b:
   # a was true, but b wasn't
else:
   # none of the conditions matched

In your code, if it wasn't for the return statement in each if, and the fact that you are checking the same two variables, it would be possible for two if statements to evaluate to true.

if a:
   # this will run if a was true
if b:
   # regardless of a this will run if b was true
else:
   # regardless of a this will only run if b was false

Also, take a look at this: https://docs.python.org/3/tutorial/controlflow.html

Comments

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.