1

I recently saw a question on the stack asking why the wrong if statement was being executed. Although I knew what to do to fix the code, i dont understand why the bitwise and is returning a true value.


win = 5
loss = 5

val = win>0 & loss == 0
print(val)
# True

val = (win>0) & (loss == 0)
print(val)
# False

val = win>0 and loss == 0
print(val)
# False
Ritwick Jha
  • 340
  • 2
  • 10

1 Answers1

1

You should check Python operator precedence, following the example you can see that is equivalent saying:

val = win > (0 & loss) == 0

as we evaluate the expression, (0 & loss) becomes 0 that is smaller than win and equal to zero.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
XxJames07-
  • 1,833
  • 1
  • 4
  • 17