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

Stephanie Marson
7,627 Pointsfar_away challenge works in cmd prompt
Hello! I can not figure out what is wrong with my code. It works in the command prompt but not within treehouse.
Code:
import datetime
def far_away(x):
td = datetime.timedelta(x)
nt = datetime.datetime.now() + td
return nt
Any help is appreciated. Thank you
4 Answers

Kenneth Love
Treehouse Guest TeacherYou need to tell timedelta
what unit it's using. You just said x
but x
could be hours, days, minutes, seconds, microseconds, etc.
EDIT:
Sorry, I was answering this in a hurry. My answer is correct (your timedelta
needs a unit), but that has nothing to do with the code challenge.
As I mentioned in a comment below, your function is going to get a timedelta
as an argument. Add that to the datetime.now()
and return the resulting new datetime
.

Stephanie Marson
7,627 PointsKenneth- Thank you for the quick response. I had a feeling that was the issue. The challenge does not actually specify which unit I should be using....
Challenge: "Write a function called far_away that takes a timedelta. Add that timedelta to datetime.datetime.now() and return the resulting datetime object."
I've tried changing this line of my code to include units: td = datetime.timedelta(x) to td = datetime.timedelta(days = x) td = datetime.timedelta(minutes = x) td = datetime.timedelta(seconds = x) td = datetime.timedelta(microseconds = x)
None of them work for the challenge. All of these work outside Treehouse in the Command Prompt, so it would appear to be correct... Thanks for any help.

Kenneth Love
Treehouse Guest TeacherYou're right, it doesn't say. That's because your x
is a time delta already. Just add it to the datetime and return it

Ben Wong
19,426 Pointsmore help plx:) I'm still stuck:(

Nate Hawk
13,229 PointsI was confused with this, its is kind of worded funny. Faraway will be taking the argument timedelta "def far_away(timedelta):". You don't need to create a timedelta. Correct me if I am wrong, but this should help.