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

Em Noble
Em Noble
6,967 Points

can someone please assist?

not sure how to do this any other way.

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(i,st):
    st = str(st)
    td = datetime.timedelta(st=i)
    return starter + td

1 Answer

Hey Em Noble! So this one is tricky. It takes an int and a string. First, the st won't need to be converted to a string because what is entered will already be a string. But the main issue lies with this line: datetime.timedelta(st=i)

Consider that time_machine is being called with the following: time_machine(4, "minutes") time_machine(7, "hours") time_machine(1, "days") time_machine(2, "years")

This function would be trying to create a timedelta with "minutes"=4. This will not work because timedelta does not take a string. It takes days, hours, minutes as attributes. For example to do the one with hours, we would want it to say datetime.timedelta(hours, 7) rather than datetime.timedelta("hours", 7).

In order to do this we need some if and elif statements that check if the string is equal to "minutes", "hours", "days", or "years", set the timedelta or td accordingly.

Remember that if "years" is entered in, it will need to get converted to years by multiplying by 365.

Then you can return the starter + td.

Hopefully this helps!