如果else语句不适用于整数值

时间:2018-06-23 17:25:40

标签: python if-statement

我有以下代码:

-- 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()

运行此代码时,似乎无法正确检查if语句中的数字。即使PatternCount的值为8,代码似乎总是进入第一个if语句。

1 个答案:

答案 0 :(得分:2)

wthresholdcthreshold是来自命令行参数的字符串。如果要对它们进行数字比较,则需要将它们转换为数字:

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