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

Ronit Mankad
Ronit Mankad
12,166 Points

Too complicated

Can anyone please help. I can't seem to figure this out.

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(num, st):

2 Answers

Hi Ronit,

Remember that a timedelta object represents a duration, the difference between two dates or times.

see my solution below.

You can find extensive discussions on this here

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(my_integer, my_string):
    if my_string == 'minutes':
        return starter + datetime.timedelta(minutes=my_integer)
    elif my_string == 'hours':
        return starter + datetime.timedelta(hours=my_integer)
    elif my_string == 'days':
        return starter + datetime.timedelta(days=my_integer)
    elif my_string == 'years':
        return starter + datetime.timedelta(days=my_integer * 365)

# print(time_machine(my_integer=5, my_string='hours'))
Steven Parker
Steven Parker
221,451 Points

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. :fishing_pole_and_fish:
    ― Maimonides

You are right to say complicated. All the time the challenges had needed simple reasoning, and a few lines of code. This one needs you to evaluate inputs based on the arguments of your time_machine function.

If the string arg is minutes do this.. if the string arg is days do this...

This one passed it for me, its just telling the program to calculate the neccessary time to be added using the right unit of time. Remember the timedelta() method only accepts three arguments and these are days, seconds and microseconds. This is the Link (https://docs.python.org/3/library/datetime.html?highlight=timedelta#datetime.timedelta)

I wrote it like this;

''' python

def time_machine(integer,string):

if string.upper() == 'MINUTES':
    new_int = integer * 60
    new_date = starter + datetime.timedelta(seconds=new_int)
    return new_date

elif string.upper() == 'HOURS':
    new_int = integer * 3600
    new_date = starter + datetime.timedelta(seconds=new_int)
    return new_date

elif string.upper() == 'DAYS':
    new_int = integer
    new_date = starter + datetime.timedelta(days=new_int)
    return new_date

elif string.upper() == 'YEARS':
    new_int = integer * 365
    new_date = starter + datetime.timedelta(days=new_int)
    return new_date

return new_date

'''