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 Far Away

Ava Jones
Ava Jones
10,723 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,410 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 91,253 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.