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

Python Dates and Times in Python (2014) Dates and Times Time Deltas

datetime 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)

sorry for the poor formatting of this question - the code starts at import datetime and ends at far_away(5)

4 Answers

Peter Lawless
Peter Lawless
24,404 Points

Hey 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!!

Peter'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.

That worked! Thanks so much.

Kathryn

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

That didn't work for me.

Vianney Gall
Vianney Gall
5,342 Points

Have you imported datetime?

Ams BM
Ams BM
5,898 Points

having 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
Alper Ozkan
2,606 Points

did 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
Gunter Ostendorp
1,740 Points

in 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!