Home > front end >  TypeError: '>=' not supported between instances of 'str' and 'tuple'
TypeError: '>=' not supported between instances of 'str' and 'tuple'

Time:08-24

It works if I type if int(hour) >= 19: but I would like it to work with something similar to this line if current_time >= exit_time:

import time    

current_time = time.strftime('%H:%M:%S')    

exit_time = ('19:00:00','%H:%M:%S')    
hour = time.strftime('%H')     
minute = time.strftime('%M')    
second = time.strftime('%S')    

if current_time >= exit_time:    
    print ("It's time to go home")     
else:    
    print ("{} hours, {} minutes and {} seconds to go home".format(18-int(hour),
         59-int(minute), 59-int(second)))    

CodePudding user response:

Notice that you're comparing current_time (which is a string) and exit_time (which is a tuple containing 2 strings). Maybe try something like:

if current_time >= exit_time[0]:
    # your code

Since the first member of the tuple is probably what you want to compare to current_time.

Hope this helps!

CodePudding user response:

The best alternative to this is.

from datetime import datetime

current_time = datetime.now().replace(microsecond=00)
exit_time = current_time.replace(hour=19, minute=00, second=00, microsecond=00)

if current_time >= exit_time:    
    print ("It's time to go home")     
else: 
    time_delta = (exit_time - current_time).total_seconds()
    hours_left = int(time_delta // 60 // 60)
    minutes_left = int(time_delta // 60) - hours_left*60
    seconds_left = int(time_delta) - hours_left*3600 - minutes_left*60
    
    print ("{} hours, {} minutes and {} seconds to go home".format(hours_left,
         minutes_left, seconds_left))
  • Related