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

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)

4 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 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                  

A simple solution for this problem is to first check if the string is 'years' and if so change the string to 'days' since timedelta will not except years as an argument. Then multiply the integer by 365 to get the equivalent number of days for the given number of years. Finally, return the duration as the difference of the starter datetime and the timedelta of the string and integer passed to the method as a literal dictionary with the ** prefix operator to unpack the dictionary.

import datetime

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

def time_machine(integer, string):
    if string == 'years':
        string = 'days'
        integer *= 365
    return starter + datetime.timedelta(**{string: integer})
Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 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.