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!

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


I don't understand what im suppose to do for this task

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)

1 Answer

Jeff Muday
Jeff Muday
Treehouse Moderator 28,372 Points

This is testing whether you understand the idea of "datetime.timedelta." You will have to look at the documentation and it will reveal that timedelta has named arguments for minutes, hours, and days.

This can be written in many different ways, but here is a "brute force" solution.

Below is a PARTIAL solution

def time_machine(value, time_unit):
    if time_unit == 'minutes':
        td = datetime.timedelta(minutes=value)

    if time_unit == 'hours':
        # your code goes here, timedelta has an 'hours' named argument

    if time_unit == 'days':
        # your code goes here, timedelta has a 'days' named argument

    if time_unit == 'years':
        # your code goes here, YEARS has a slight trick since you need to specify in days

    # finally return starter plus our timedelta "td"
    return starter + td