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 trialAdam Vasik
11,702 PointsI have no idea how to do this please help :(
I have been trying to solve this but my way 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(**kwargs):
td = datetime.timedelta(**kwargs)
if 'years' in kwargs:
tdd = datetime.timedelta(days = 365)
return starter + (td + (tdd * getattr(other, 'years')
return starter + td
3 Answers
Pratham Mishra
14,191 Pointsdef time_machine(time_int, years): if years == 'years': time_int = 365 years = 'days' return starter + datetime.timedelta(*{years : time_int})
Steven Parker
231,275 PointsThe function should take a normal argument, you won't need "kwargs".
Another hint: you should also not need "getattr".
Rocket Dollar Invest
7,129 PointsA simple solution for this problem is to first check if the string is 'years' and if so change the string to 'days' since timedelta will not except years as an argument. Then multiply the integer by 365 to get the equivalent number of days for the given number of years. Finally, return the duration as the difference of the starter datetime and the timedelta of the string and integer passed to the method as a literal dictionary with the ** prefix operator to unpack the dictionary.
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
def time_machine(integer, string):
if string == 'years':
string = 'days'
integer *= 365
return starter + datetime.timedelta(**{string: integer})