Convert the string to an integer with int
:
hours = int("14")
if (hours > 14):
print "yes"
In CPython2, when comparing two non-numerical objects of different types, the comparison is performed by comparing the names of the types. Since 'int' < 'string'
, any int is less than any string.
In [79]: "14" > 14
Out[79]: True
In [80]: 14 > 14
Out[80]: False
This is a classic Python pitfall. In Python3 this wart has been corrected -- comparing non-numerical objects of different type raises a TypeError by default.
As explained in the docs:
CPython implementation detail: Objects of different types except
numbers are ordered by their type names; objects of the same types
that don’t support proper comparison are ordered by their address.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…