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

igor corrales
igor corrales
2,474 Points

Problem with timedelta, can anyone help me please ?

Probably I don't understand the exactly meaning of the exercixe ....because I get an object but I get an error.

far_away.py
import datetime

def far_away(number):
    a = datetime.datetime.now() + datetime.timedelta(days=number)
    return a 

2 Answers

David Smith
David Smith
10,577 Points

You were almost there,

The quiz asked that the function take a time delta as an argument, not an int. All you have to do is;

import datetime

def far_away(number):
    a = datetime.datetime.now() + number
    return a 

To help you understand I've modified some of the names

import datetime


# define the function and allow it to accept an argument. An argument is a variable that is used within a function.
def far_away(time_delta_argument):
    # here we're setting a variable to the current time plus a timedelta
    # a time delta is "datetime.timedelta(hours=7)"
    datetime_datetime_now_plus_timedelta_argument = datetime.datetime.now() + time_delta_argument
    # here we return the variable we set before
    return datetime_datetime_now_plus_timedelta_argument


# this is not part of the challenge but I included it to help demonstrate how this function would be used 
print(far_away(datetime.timedelta(hours=7)))
igor corrales
igor corrales
2,474 Points

Thanks a lot for the explanation !!!

David Smith
David Smith
10,577 Points

You're welcome Igor, we've all been there :)