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) Let's Build a Timed Quiz App Harder Time Machine

Holden Glass
Holden Glass
6,077 Points

Not sure how it wants me to return the data

I have played around with this and I don't know how it wants me to output the data. In the example it just has a single datetime and I don't know how to get it like that. I am sure as soon as someone posts an answer for this question I'll get it but until then, I'm kinda stuck.

time_machine.py
import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)

# Remember, you can't set "years" on a timedelta!
# Consider a year to be 365 days.

## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)

def time_machine(number, string):
    if string == 'years':
        the_days = 365 * number
        return datetime.datetime(starter + datetime.timedelta(days=the_days))
    elif string == 'days':
        return datetime.datetime(starter + datetime.timedelta(days=number))
    elif string == 'hours':
        return datetime.datetime(starter + datetime.timedelta(hours=number))
    elif string == 'minutes':
        return datetime.datetime(starter + datetime.timedelta(minutes=number))

1 Answer

Steven Parker
Steven Parker
231,248 Points

You're doing more work than you should.

Remember, if you already have a datetime (like starter), and you add a timedelta to it, the result is also a datetime. You can return that directly.

Holden Glass
Holden Glass
6,077 Points

I tried to do that to. I would just take away the datetime.datetime at the beginning right?

Steven Parker
Steven Parker
231,248 Points

Yes, and then you can remove the parentheses around the expression.