-2

I was doing some tests, when I tried:

len(pin) == (4 or 6) 

Half of the tests failed.

However with :

(len(pin) == 4 or len(pin) == 6)

All the tests were passed.

I am unable to understand the difference that is between these two. pin is usually a number like 1234 or 12345.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40
  • Why downvote and duplicate? as this condtion `len(pin) == (4 or 6)` is different than the question to which my question as stated as duplicate of. – Shubham Namdeo May 08 '17 at 05:59

1 Answers1

2

That is because according to precedence rules, the right hand side expression is evaluated first in your first condition i.e.

len(pin) == (4 or 6)

Here, first (4 or 6) is evaluated and returns 4 (or true in some languages). Now, only those cases return true where length actually is 4.

Your second condition works as expected, because it compares the length to 4 and 6 both separately and then applies an or on both the boolean values.

Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64