Reputation: 1986
I have one program which parse the output on run, compare the time from that with given value and prints the difference, but it is not working as per my way:
a = 'Time Taken for Response: 31 msec'
time_out = 75
if a.split()[4] > time_out:
print "time taken is more than given conditions"
print a.split()[4]
OUTPUT IS AS BELOW:
time taken is more than given conditions
31
I do not understand as why the program goes into the loop itself when 31 < 75
Any clue or guidance???
Upvotes: 0
Views: 90
Reputation: 1080
You're comparing a string with an integer.
The binary representation of your string is a larger binary number than the binary representation of your decimal number.
Note: information in another answer indicates that the above is a factually inaccurate explanation when discussing the python interpreter
Converting to int as in
if int(a.split()[4]) > time_out:
should give you the correct answer.
Incidentally, if you use python 3 rather than python 2, trying to compare a string and int will give you the following error:
TypeError: unorderable types: str() > int()
which better meets user expectation
Upvotes: 3
Reputation: 852
After splitting you are actually comparing a string and an int. Unfortunately what this does is completely bizarre in CPython before 3.0 and is based on the type name of all things, not the content or binary representation. See How does Python compare string and int?
Upvotes: 2
Reputation: 132018
You are comparing "31" and 75. Try int(a.split()[4]) > time_out:
instead.
Upvotes: 6