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

Write a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". This d

Hey there, Struggling with datetimes in general. It Will def be a subject I come back to again. Can't figure out how to calculate "years"... please help.

Thanks, Matt

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(take_int, take_string):
    if take_string == 'minutes':
        offset = datetime.timedelta(minutes=take_int)
    elif take_string == 'hours':
        offset = datetime.timedelta(hours=take_int)
    elif take_string == 'days':
        offset = datetime.timedelta(days=take_int)
    else:
        offset = datetime.timedelta(days,365=take_int)
    return starter + offset

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Try passing in a little calculation to the else block. All you need to do is multiply the timedelta day by the integer 365

Jonathan Grieve would that be before the "offset = datetime.timedelta(days,365=take_int) part... sadly I'm still not getting it

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I'll give you a little bit more of a nudge. :)

else:
        offset = datetime.timedelta(days=take_int*365)

It's just like what you did before in the last else if except in this block, if none of the other conditins match you're getting the day timedelta and simply multiplying that value by 365.

Jonathan Grieve thanks! I'm going to be watching and rewatching the DateTime videos... for some reason, it's a concept that isn't sticking. Has anyone else struggled with this set of videos by Kenneth Love?

nicolea
nicolea
5,048 Points

Here's my response:

def time_machine(take_int, take_string): if take_string == 'minutes': offset = datetime.timedelta(minutes=take_int) elif take_string == 'hours': offset = datetime.timedelta(hours=take_int) elif take_string == 'days': offset = datetime.timedelta(days=take_int) else: offset = datetime.timedelta(days=take_int*365) return starter + offset