0

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???

austin
  • 5,816
  • 2
  • 32
  • 40
user2033758
  • 1,848
  • 3
  • 16
  • 16

3 Answers3

6

You are comparing "31" and 75. Try int(a.split()[4]) > time_out: instead.

sberry
  • 128,281
  • 18
  • 138
  • 165
3

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

Community
  • 1
  • 1
Kiirani
  • 1,067
  • 7
  • 19
2

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?

Community
  • 1
  • 1
John Luebs
  • 861
  • 6
  • 8