Reputation: 87
Write an hours2days function that takes one argument, an integer, that is a time period in hours. The function should return a tuple of how long that period is in days and hours, with hours being the remainder that can't be expressed in days.
My code looks like this:
def hours2days(hours):
days = hours // 24
hours = hours % 24
print(days, hours)
hours2days(39)
anyone has any other suggestions?
Upvotes: 0
Views: 60
Reputation: 23443
I thought this could be more explicit about the result.
def hours2days(hours):
days, hours = divmod(hours, 24)
return {"days": days, "hours": hours}
>>> hours2days(39)
{'days': 1, 'hours': 15}
Upvotes: 0
Reputation: 42017
Yes, I have one -- use divmod
:
In [16]: def hours2days(hours):
...: return divmod(hours, 24)
...:
In [17]: days, hrs = hours2days(39)
In [18]: days
Out[18]: 1
In [19]: hrs
Out[19]: 15
Upvotes: 1