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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Adam Vasik](https://uploads.teamtreehouse.com/production/profile-photos/11528082/micro_402d0d6a-925f-46e0-bb62-3f79cdb1795a.jpg)
Adam 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](https://uploads.teamtreehouse.com/production/profile-photos/11534545/micro_p_logo.jpg)
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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe function should take a normal argument, you won't need "kwargs".
Another hint: you should also not need "getattr".
![Rocket Dollar Invest](https://uploads.teamtreehouse.com/production/profile-photos/10750982/micro_rd-logo-fullColor.jpg)
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})