Jecki Jecki - 6 months ago 25
Python Question

python - if statement return wrong value

I used the followng code to trigger IF statement based on the value of row[0] my problem is always IF statement give me wrong output even if the value is greater than 0 its keep printing NO FAILURE .

conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("select count(*) as result from events_log where match_event_timestamp > (NOW() - INTERVAL '147 hours');")
row = cursor.fetchone()
cursor.execute("SELECT COUNT(*) AS result FROM events_log WHERE status = 'FAILURE' AND match_event_timestamp > (NOW() - INTERVAL '24 hours');")
row += cursor.fetchone()
logger.info("Number of Events in last 24hr : %s ; Number of Event FAILURE in last 24h: %s" % (row[0],row[1]))
output = { 'api_key':'jhfsdf', 'data': { 'item' : [
{'text': 'Today ', 'value': row[0]},
{'text': 'Current Month ', 'value': row[1]}
] } }

if row[0] > '0':
print('FAILURE')
else:
print("NO FAILURE")


what did i missed in IF statement here ?

Answer

'0' is a string use 0. Like this:

if( row[0] > 0 ):
   ...

Otherwise you get this:

TypeError: unorderable types: int() > str()

which means you may not compare an int to a string.