Aaron Dank
Aaron Dank

Reputation: 37

Comparing int values

Why is my code not letting me compare these 4 integers--- it just returns none? What am I doing wrong -- just a beginner

def isinRange(exonStartPos,   exonEndPos,  ChromListStartPos,  ChromListEndPos):
    inRange  = False
    if exonStartPos < ChromListStartPos & exonEndPos < ChromListEndPos:
        returnList = [inRange, 0 , 0]
        return returnList
    if exonStartPos > ChromListEndPos & exonEndPos> ChromListEndPos:
        returnList = [inRange, 0 , 0]
        return returnList
    if exonStartPos < ChromListStartPos & exonEndPos < ChromListEndPos:
        inRange = True
        returnList = [inRange, ChromListStartPos, exonEndPos]
        return returnList
    if exonStartPos > ChromListStartPos & exonEndPos > ChromListEndPos:
        inRange = True
        returnList = [inRange, exonStartPos, ChromListEndPos]
        return returnList
    if exonStartPos == ChromListStartPos & exonEndPos <= ChromListEndPos:
        inRange = True
        returnList = [inRange, exonStartPos, exonEndPos]
        return returnList
    if exonStartPos >= ChromListStartPos & exonEndPos == ChromListEndPos:
        inRange = True
        returnList= [inRange, exonStartPos, exonEndPos]
        return returnList


if __name__ == '__main__':
    ChromListRange = isinRange(665973, 666002, 745554,752391)
    print ChromListRange[0]

Upvotes: 1

Views: 64

Answers (2)

Baldrickk
Baldrickk

Reputation: 4409

Operator precidence.

Observe the following:

>>> 2 < 3 and 1 < 2
True
>>> 2 < 3 & 1 < 2
False

The first statement evaluates to:

(2<3) and (1<2)
True  and True
True

The second however evaluates to:

2 < (3 & 1) < 2
2 <    1    < 2
False

a<b<c is valid syntax in python (in a language like C, this would be a syntax error) so no error is thrown, you just get unexpected output.

Logical algebra should be done with and or and not, instead of the bitwise equivalents.

Upvotes: 0

cookiedough
cookiedough

Reputation: 3832

I believe you need to change your &s to and. It worked for me.

Upvotes: 2

Related Questions