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

Support with Time_Machine quiz

I am not sure how to pass the string as an argument into the timedelta function. I have attached my code which looks overly complicated and wrong!

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(my_int, my_str):
    if my_str.upper() == "YEARS":
        my_str = "days"
        my_int += 365

    dt = datetime.timedelta(**{my_str:my_int})
    return starter - dt

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Stuart,

Actually your code looks pretty good. There are just 2 minor things that are holding it back from passing, but your logic is solid. Your dictionary unpacking in the timedelta is perfectly valid.

First, when you are converting years to days, my_int will need to be multiplied by 365 using the multiplication assignment operator (*=), not addition assignment.

And second, the challenge is looking for a date sometime in the future from "starter", so you will need to use addition in your return statement instead of subtraction.

Good luck.

Thanks so much for your help.

Happy New Year