1

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.

2
  • wthreshold is not a number. It's a string. Convert it to a number with int() Commented Jun 23, 2018 at 17:27
  • 1
    Switch to Python 3, and you'll get TypeError: '<' not supported between instances of 'int' and 'str' instead of weird results. It's been out for nearly seven years. Commented Jun 23, 2018 at 18:11

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

scriptname, feature, logfile, daterange, stringA, stringB, wthreshold, cthreshold, option = sys.argv (How would this work if the number of parameters change as is shown here?)
@swenson Just add more variables to the left side of the operation. Alternatively, you could just explicitly extract them from arg. E.g.: someVariable = int(sys.argv[2].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.