-4

I have an array of two int's, and I want to check if either is None, so I have this:

print hourArray
if hourArray[0] or hourArray[1] is None:
    print "FAILED???"
else:
    print "array is full"

And even though the print hourArray shows this right before the if statement

[2040, 2640]

It prints FAILED??? even though neither of the elements in the array is None?

Why is this happening?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
james red
  • 41
  • 1
  • 8
  • `if (hourArray[0] is None) or (hourArray[1] is None)` or `if None in [hourArray[0], hourArray[1]]` – Martin Thoma Jun 23 '15 at 18:25
  • `if a or b == something` is an incredibly common mistake in Python, see the duplicate. Firstly it should be `if a == something or b == something`. Secondly you should use `==` instead of `is` – Cory Kramer Jun 23 '15 at 18:25
  • @CoryKramer Comparing to singletons like `None` should be done with `is` in python, according to [pep8](https://www.python.org/dev/peps/pep-0008/). – jme Jun 23 '15 at 18:27

1 Answers1

2

The issue is that you are checking if (hourArray[0]) or (hourArray[1] is None) , all non-zero integer values are always true.

You should do -

if hourArray[0] is None or hourArray[1] is None:

Example of non-zero integer values being true -

>>> if 1:
...     print('Hello')
...
Hello
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176