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

Emily Strobl
Emily Strobl
5,036 Points

Confused

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

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)

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,401 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