In python if you write something like
foo==bar and spam or eggs
python appears to return spam if the boolean statement is true and eggs oth
Try using parentheses to make the expression non-ambiguous. The way it is, you're getting:
(foo == bar and spam) or eggs