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

Python far_away challenge seemingly does not provide enough context on the datetime being considered by the challenge.

The description of the challenge is very vague on what datetime value an instance of timedelta should subtract from.

Considering the datetime module isn't even imported, it seems some existing code was intended to be available to challenge takers.

Kenneth Love, any tips on solving this particular challenge?

import datetime
def far_away(timeDelta):
  return datetime.datetime.now() - timeDelta

10 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's meant to be that far away from now. I've updated the code challenge with that. Let me know if you think it's still not explicit enough.

Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

They are difficult to read and understand what the challenges are asking for sometimes. Though if you stick with it and work through each line one by one, they usually come clear. It takes patience. However it is of course possible that there are questions we just don't get what they are asking us to do. Nobody is to blame, and I think Treehouse appreciates our feedback. That is what makes us all better as students and as teachers. Here is my code used to solve this challenge.

import datetime

def far_away(x):
    return x + datetime.datetime.now()
Neil Gordon
Neil Gordon
8,823 Points
import datetime

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

'''
Jose Luis Lopez
Jose Luis Lopez
19,179 Points

import datetime

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

I get the impression that I am not the only person who read the far_away code challenge and had no clue what I was being asked to do. A concrete sink-or-swim approach where you are asked to create a function that returns the end time of a t-minute appointment would have been clearer than what the code challenge now says.

Here is what I might have done differently if it were my course:

  1. The code challenge following the first video should have included questions on timedelta objects.
  2. There should be explicit code challenge questions on each of the following points, since they are key and easy for beginners to miss:
    • timedelta objects can be created by taking the difference of two datetime objects.
    • timedelta objects display different attributes than datetime objects do.
    • Nonetheless, timedelta objects can be added to/subtracted from datetime objects, and the result is a datetime object.
    • timedelta objects can also be created directly.
    • timedelta objects may be assigned attributes (such as hours) which are not among the displayed attributes, but which will be converted to one of the displayed attributes.
  3. I would step through some variation of the appointment example in a code challenge before asking an abstract question involving some unspecified "timedelta."

I think the Python courses are in general badly organized with unclear expositions in the videos, not enough reinforcement in the exercises, and confusing code challenges. Touching up the code challenges isn't enough. These courses should be redone.

Or instead of TH redoing the courses...if you don't like them then don't do them! No one is forcing you to :) I personally think the courses are very well laid out Kenneth Love!

As the adage goes and T-Swift once said...haters gonna hate

@Trevor Collins: I'm hoping you have enough class to reconsider your juvenile and grossly inappropriate comment and delete it. Your brown-nosing is belied by the fact that you obviously searched the forum to get clarification on this code challenge because you didn't understand it either.

Devin Gyug
Devin Gyug
6,208 Points

Iam a beginner for the most part and for awhile I found the challenges to be frustrating because small things kept me from getting the correct answers. However I realize now it forces me to go out and find more information online for each new subject. That has helped me learn alot more. I get the feeling many programmers do that in the real world as well.

Andrew Merrick
Andrew Merrick
20,151 Points

Kenneth/Kevin:

I'm a bit confused since I originally overthought how to solve the challenge and I feel my reverse course of "keep it simple stupid" may be an answer that passes the challenge but does not imply a correct understanding. Without giving away the answer...

# Write a function called far_away that takes a timedelta

import datetime

def far_away(tdelta):

# Add that timedelta to datetime.datetime.now() and return the resulting datetime object
    return tdelta + datetime.datetime.now()

This code passed the challenge since it's all that's being asked, but I feel that it may be so simple that it doesn't show my understanding of the material. It may be that the challenge is simply saying to return x + y, where x is a timedelta (i.e. datetime.timedelta(hours=9) and y is datetime.datetime.now()) and the challenge fills in the variables with backend code.

Is that correct? Was I just overthinking the solution?

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, that's all it's meant to do. Basically just shows that you understand that timedeltas and datetimes can be added together.

Another completely trivial exercise that took forever to do because it was badly posed.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm always happy to get suggestions on how to make code challenges and quizzes easier to understand or more clear. What do you think was phrased badly in this one?

This passed, just read it as it states

import dateline def far_way(timedelta): return datetime.datime.now() + timedelta

check your spellings python is case sensitive otherwise your answer is correct

Hey, Kenneth Love:

I appreciate the prompt answer.

It's still somewhat unclear what to do after the arthmetic. From my understanding, it returns a datetime object from the operation while the challenge still states it wants a datetime object.

It's unclear based on the previous videos so far (unless I extremely snoozed during the last video) how to change it to a datetime object if i'm incorrect, and whether that makes sense for python to convert a timeDelta to a datetime explicitly.

If two datetime objects being added/substract result into a timedelta and a datetime +/- a timedelta does indeed equal a datetime, I'm confused why a return statement doesn't in fact return a datetime object that's correct.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm able to pass with, effectively, return datetime.datetime.now() + <passed-in timedelta>. What are you trying that isn't passing?

EDIT: Updated the description a bit more.

Kenneth Love: That's exactly what I tried.; I've called the passed in timeDelta and substracted it from.... Doh. I finally get it, the challenge had asked for the timedelta object to be added to a datetime object, not subtracted from a datetime object.

Thanks in your part for helping me realize the error of my ways.