Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Let's Build a Timed Quiz App Harder Time Machine

matthew mahoney
seal-mask
.a{fill-rule:evenodd;}techdegree
matthew mahoney
Python Development Techdegree Student 2,536 Points

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

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 89,889 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
Jonathan Grieve
Treehouse Moderator 89,889 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.

matthew mahoney
seal-mask
.a{fill-rule:evenodd;}techdegree
matthew mahoney
Python Development Techdegree Student 2,536 Points

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?