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 trialKathryn Klarich
4,622 Pointsdatetime challenge
the challenge is: Write a function called far_away that takes one argument, a timedelta. Add that timedelta to datetime.datetime.now() and return the resulting datetime object.
My code seems to work in work spaces, but when I try to submit it for the challenge I get "Bummer try again". Could someone help me identify what's wrong?
see code below:
import datetime
def far_away(time):
time_change = datetime.timedelta(hours=time)
future = datetime.datetime.now() + time_change
return future
far_away(5)
4 Answers
Peter Lawless
24,404 PointsHey Kathryn - I think the issue is that your function's argument is supposed to be a timedelta object but instead it accepts an integer value and uses that as an 'hours' parameter for a timedelta object created within the function. I think it would do to eliminate your time_change line and simply have:
future = datetime.datetime.now() + time
Let me know if this works!!
Gary Lehman
2,387 PointsPeter's solution worked for me, but I don't understand why. The challenge is calling for a timedelta to be used as an argument, which this solution does not. It also asks that we add that timedelta to datetime.datetime.now(), which it does not.
Kathryn Klarich
4,622 PointsThat worked! Thanks so much.
Kathryn
Khaleel Yusuf
15,208 PointsThat didn't work for me.
Vianney Gall
5,342 PointsHave you imported datetime?
Ams BM
5,898 Pointshaving issues with this one, it works on my IDE and the workspace, but keep on getting
Bummer: Try again!
my code is below, what am I doing wrong???
import datetime
def far_away(days = 0):
return datetime.timedelta(days) + datetime.datetime.now()
far_away(1)
I tried to keep my code clean.
Alper Ozkan
2,606 Pointsdid you find the answer , l tried so many code , ı didn't find , some one help you ??
any way this is my code but doesn't work
import datetime
def far_away (alpi): days = datetime.datetime (hours = alpi) finish = datetime.datetime.now() + days return f'{finish} '
far_away(4)
Gunter Ostendorp
1,740 Pointsin your function it looks like you're almost trying to define a variable instead of passing in an arguement, instead try passing in just "time", then in the body of your function you could store your timedelta in a variable then set that variable equal to time + datetime.datetime.now() then return that variable.
Hope it helps!
Kathryn Klarich
4,622 PointsKathryn Klarich
4,622 Pointssorry for the poor formatting of this question - the code starts at import datetime and ends at far_away(5)