Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Far Away

Ava Jones
Ava Jones
2,662 Points

TypeError: far_away() missing 1 required positional argument: 'timedelta'

please help

far_away.py
import datetime
def far_away(timedelta):
    datetime.timedelta() + datetime.datetime.now()
    return datetime.datetime.now()
far_away()

2 Answers

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

Hi Ava! Check out this line of code:

def far_away(timedelta):

The challenge says "write a function called far_away that takes one argument, a timedelta." You did that perfectly.

Next, it says: "Add that timedelta to datetime.datetime.now()"

Here's your problem:

def far_away(timedelta):
    datetime.timedelta() + datetime.datetime.now()

Look at the second line of code. That datetime.timedelta() is a different object. It's not the timedelta you're passing in as an argument.

You can reference that timedelta anywhere in your function by simply writing "timedelta".

Like this:

import datetime

def far_away(timedelta):
    return timedelta + datetime.datetime.now()

Lastly, you called the function in your original code (in the last line.)

import datetime
def far_away(timedelta):
    datetime.timedelta() + datetime.datetime.now()
    return datetime.datetime.now()
far_away()

You don't need to call the function. When you press "Check Work," the Treehouse Challenge calls the function behind the scenes.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,845 Points

You're specifying the timedelta as an argument but you don't need to call the function at the end of your code. That's why it's not working because it's expecting a value to be passed in.

It can be accomplished with one line of code in a function

import datetime

def far_away(timedelta):
    return timedelta + datetime.datetime.now()

Add the current datetime value to your timedelta argument.