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

Have been having trouble with this (easy) code challenge. Have been stuck on it for a few days now. Any help?

I managed to complete the previous similar code challenge straight away which involved using a function taking one argument and a timedelta. All that was then need was to return the addition of the timedelta to starter.

This challenge is more difficult - taking two arguments. Firstly I am not really sure how or why you would need to have two arguments. To make a timedelta you can set the value of the time change without needing an argument. Also having a string in your parenthesis at the beginning looks wrong to me (5, "minutes") as you can see in the example! I thought the whole point of functions was that you made sure variables were your arguments so that you can pass in whatever you like, not strings/integers that you cannot.

Or is it that you need to convert the timedelta (that results from subtracting datetimes) back into a datetime at the end with an strptime?

If someone could help out would be handy as have spent days on this and now think that as well as not getting datetime/timedeltas I now don't understand functions either! Kenneth Love

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(int1, minutes):
    mins_ahead = datetime.timedelta(minutes=int1)
    total_delta = starter + mins_ahead
    return total_delta - mins_ahead

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It won't always be minutes, though. The second argument is the type of time that the first argument is. So if the first argument was 10 and the second was "years", you should return a new datetime that's 10 years ahead of starter. If they were 5 and seconds, then your new datetime is 5 seconds ahead.