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

Not sure why this won't complete. Quick helped needed for challenge!!

In my terminal I can create a timedelta with hours, seconds, and minutes. The only thing I cannot create is a timedelta with years. If I convert that to days and keep everything else the same this should submit

Is there something I am overlooking right now? Could use some help

time_machine.py
import datetime

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

def time_machine(num , time):
    if time == "years":
        days = num * 365
        change_in_time = datetime.timedelta("days" = days)    
    else:
        change_in_time = datetime.timedelta(time = num)
    return starter + change_in_time

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Two errors. The first timedelta Needs to be call with the keyword days without quote marks.

        change_in_time = datetime.timedelta(days=days)

The other timedelta needs to be called for each type of "time" when not years. You could build a if/elif/else to include checking to match "minutes", "hours", "days". Alternatively, you can put all three in one statement:

        change_in_time = datetime.timedelta(**{time: num})

Bonus tip: it is best to avoid using the name of built-in functions as variable or parameter names. "time" is a built-in function.

Stuart Wright
Stuart Wright
41,120 Points

Nice solution, didn't realise there was a way to do this without elif blocks.

Stuart Wright
Stuart Wright
41,120 Points

The issue here is that you cannot pass a string as the name of the argument in timedelta. So, where you have written:

datetime.timedelta("days" = days)

You instead need:

datetime.timedelta(days=days)

Unfortunately this means that your else block doesn't work properly either, because you're essentially still doing the same thing as above. I can't think of a way to do this without some elif blocks. For example:

elif time == "minutes":
    change_in_time = datetime.timedelta(minutes=num)

Hopefully this is enough to get you started. Let me know if you need any more help.

Edit to add: see Chris' answer for a more elegant solution that doesn't involve elif blocks.