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 trialJonathan Shedd
1,921 PointsNeed help with this challenge that has timedeltas in it.
I'm not sure what's wrong with my code. It keeps returning an error message regarding the keyword "time." Any help is appreciated!
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
def time_machine(int1, time):
if time == "years":
time_ahead = datetime.timedelta(days=365*int1)
return(starter + time_ahead)
else:
time_ahead = datetime.timedelta(time=int1)
return(starter + time_ahead)
# 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)
1 Answer
Steven Parker
231,248 PointsWhen passing arguments by keyword, the keyword must be literal.
On this line, "time" is a variable that contains the keyword:
time_ahead = datetime.timedelta(time=int1)
But "time" is not itself a keyword of a timedelta argument. You might need a chain of elseif to be sure the proper argument is being passed.