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

help here please

Write a function called far_away that takes one argument, a timedelta. Add that timedelta to datetime.datetime.now() and return the resulting datetime object.

far_away.py
def far_away():
    import datetime
    now = datetime.datetime.now()
    return datetime

5 Answers

Remember - Imports like to go up top so that's where I begin..even if they don't HAVE to, that's where this course has been placing them.

If you start by importing the datetime at the top and begin you will have a better chance. What I did was simply define the function far_away, inserted the argument 'timedelta' and closed ':'.

I then created a variable to hold the argument + the datetime.datetime.now()

I then simply returned that variable.

Your function should take one argument and it takes 0. You are importing the datetime module, use it to build an object with the current local time and return the datetime module at the end. You are very far off from what's being asked. I suggest you take a step back and master the basics before delving into the datetime library otherwise I think you'll be even more confused.

Anupam Kumar
Anupam Kumar
3,795 Points

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

Why is the problem with this code??

Almost there. You don't need to initialize/construct the timedelta because you already receive it via the function argument. You should use it straight away as is.

Anupam Kumar
Anupam Kumar
3,795 Points

Any solution or help on this, Does anyone here to help >>

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

Why? I rewatched the videos several times, I ran similar code in pycharm, just...why??