4

We already know that, datetime.timedelta(hours= 5) is a proper syntax. How is it possible to pass an argument that can replace 'hours' as given in the function down below?

    def check_time(until_when, def_time, how_long):
            if until_when- datetime.datetime.now() > datetime.timedelta(def_time = how_long):
            input('Task has been finished.\nPress any key to quit\n')
            exit()  
Ali
  • 335
  • 1
  • 3
  • 12

1 Answers1

8
def check_time(until_when, def_time, how_long):
    arg_dict = {def_time:how_long}
    dt = datetime.timedelta(**arg_dict)
    if until_when- datetime.datetime.now() > dt:
        input('Task has been finished.\nPress any key to quit\n')
        exit()

See Understanding kwargs in Python and the tutorial.

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199