Ali
Ali

Reputation: 335

how to pass argument to datetime.timedelta

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()  

Upvotes: 4

Views: 2686

Answers (1)

tacaswell
tacaswell

Reputation: 87376

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.

Upvotes: 8

Related Questions