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

Stuck on challenge

I'm just wondering what I'm doing wrong here.

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.
def time_machine(inte, stri):
    s = stri
    if s == "year":
        return starter + datetime.timedelta(vars()[s] = inte * 365)
    else:
        return starter + datetime.timedelta(vars()[s] = inte)


## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

Hey Jackson Porter ! :wave:

You're very close! We can even shorten it down further here as well. I believe the issue is the string we're checking on and the vars()[] which I'm personally not familiar with.

First, we need to be checking for the string of "years" (plural) instead of "year". If it is "year", we can just re-assign the s variable's value to "days". Then we can re-assign the inte variable's value to inte = inte * 365 or inte *=365.

Then outside of that if, we can utilize the ** unpacking operator like so...

return starter + datetime.timedelta(**{s: inte})