Sep 3, 2009

-1 is true (if loops)

In programming (at least in python), all values except 0, empty strings, etc is true.
That is, if your expression returns something like '-1', your code inside the if loop is still run.

E.g.
myString = 'Hello World'
if myString.find('bye'):
print('I found bye.')

Output:
> I found bye.

This is because the .find command returns '-1', which is 'true'.

Instead, you need to replace the if statement to something like so:
if (myString.find('bye') != -1):

(Thanks to Stephen Hansen.)