Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hunter Kiely
2,768 PointsI need help understanding an invalid keyword argument in time_machine.py
After looking at many other members posts regarding this same challenge, I am not sure if I am just approaching the task differently or am completely off base. Either way I am not sure why this doesn't work.
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
# Remember, you can't set "years" on a timedelta!
# Consider a year to be 365 days.
## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)
def time_machine(number, MHDY):
if MHDY == 'years':
number = (number * 365)
MHDY = 'days'
elif MHDY == 'months':
number = (number * 12)
MHDY = 'days'
elif MHDY == 'weeks':
number = (number * 52.1)
MHDY = 'days'
end_time = datetime.timedelta(MHDY = number)
duration = starter - end_time
return duration
1 Answer
Iain Diamond
29,379 PointsHi Hunter,
The way I approached this problem was relying on the fact that adding datetimes and timedeltas is easy. So given a starter datetime, you could simply finish with:
return starter + delta
this would lead to the if statements being used to construct different timedeltas depending on the string type being passed into the function, e.g.
if time_string == 'minutes':
delta = datetime.timedelta(minutes=num)
Once this is sorted, the only slightly tricky part is what to do about "years". Remembering that timedeltas can be constructed using minutes, hours, and days.
Please, let me know how you get on. iain
Hunter Kiely
2,768 PointsHunter Kiely
2,768 PointsI figured it out. Thanks for your help!