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

This is confusing

How should I go about this challenge

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)

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,879 Points

A typo crept into your code with a parenthesis... see solution below.

The variable delta is the generic timedelta. Also, years is not a parameter of timedelta so we have to use the trick of converting years into days.

days = x * 365

change hourdelta to delta

change daydelta to delta

change yeardelta to delta

This solution below will work

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(x, time_unit):
# The line above is where the syntax error is

    if time_unit == "minutes":
        # this is how to compute a time "delta" for minutes
        delta = datetime.timedelta(minutes=x)

    if time_unit == "hours":
        delta = datetime.timedelta(hours=x)

    if time_unit == "days":
        delta = datetime.timedelta(days=x)

    if time_unit == "years":
        # here is the tricky part, timedelta doesn't support years parameter
        # so we convert it to days (e.g. 365 days in a year)
        x =  x * 365
        delta = datetime.timedelta(days=x)


    # return the value of the starting date and a delta
    return starter + delta                  
Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,879 Points

This is an interesting challenge-- they want you to create a function that returns a NEW Python datetime type based on a number and a string which represents a unit of time (e.g. minutes, hours, days, years).

You should refer to the documentation on datetime to learn more:

https://docs.python.org/3/library/datetime.html

There are a number of different ways to do this. There are some really compact solutions, but I am trying to keep it simple.

BELOW IS NOT A COMPLETE SOLUTION, BUT A SUGGESTION

def time_machine(x, time_unit):

    if time_unit == "minutes":
        # this is how to compute a time "delta" for minutes
        delta = datetime.timedelta(minutes=x)

    if time_unit == "hours":
        # you fill in the code to make this work
        pass

    if time_unit == "days":
        # you fill in the code to make this work
        pass

    if time_unit == "years":
        # this is the tricky one, since years must be converted to days
        pass

    # return the value of the starting date and a delta
    return starter + delta

Hi Jeff! Thank you for your assistance but for some reason, I am given a syntax error.

import datetime

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

def time_machine(x, time_unit):
# The line above is where the syntax error is

    if time_unit == "minutes":
        # this is how to compute a time "delta" for minutes
        delta = datetime.timedelta(minutes=x)

    if time_unit == "hours":
        hourdelta = datetime.timedelta(hours=x)

    if time_unit == "days":
        daydelta = datetime.timedelta(days=x)

    if time_unit == "years":
        years = days * 365
        yeardelta = datetime.timedelta(years=x)


    # return the value of the starting date and a delta
    return starter + delta                            

# 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)

I also feel a little lost in the recent challenges. So any guidance would be greatly appreciated.