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

far_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
STAFF
Kenneth Love
Treehouse Guest Teacher

You 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.

Kenneth- 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
Kenneth Love
Treehouse Guest Teacher

You'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

more help plx:) I'm still stuck:(

I 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.