简体   繁体   中英

if else statement not working on integer values

I have the following code:

-- content of sys.argv is 2 and 10 which is assigned to the specified variables. 

wthreshold, cthreshold = sys.argv
def Alerting():
    if PatternCount < wthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(0)
    elif PatternCount >= wthreshold and PatternCount < cthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(1)
    elif PatternCount >= cthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(2)
    else:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(3)


LRangeA = """this line 1
another line 2
one more line 3
line 4 
line 5
line 6
line 7
line 8"""
PatternCount = len(LRangeA.split('\n'))
Alerting()

When I run this code, it doesn't seem to be correctly checking the numbers in the if statements. The code always seems to go into the first if statement even though the value of PatternCount is 8.

wthreshold and cthreshold are strings coming from the command line arguments. If you want to compare them numerically, you need to convert them to numbers:

wthreshold, cthreshold = [int(x) for x in sys.argv]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM