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

Robert Raiz
Robert Raiz
2,811 Points

Bummer! `time_machine` didn't return the right datetime.

Hello, Tried to run my script in the treehouse code challenge and I get the error from the subject line. I created a script to test it out and it works from the interpreter. I am not sure what am I doing 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.

def time_machine(nbr, t_me):
    if t_me == 'years':
        result = starter + datetime.timedelta(days = nbr * 365)
        return result
    elif t_me == 'minutes':
        result = starter + datetime.timedelta(minutes = nbr)
        return result
    elif t_me == 'hours':
        result = starter + datetime.timedelta(hours = nbr)
        return result

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

2 Answers

Kent Åsvang
Kent Åsvang
18,823 Points

I think the problem with your python is that you call the function yourself, this will confuse treehouse code that checks for the correct output, since your output also is included. Try removing this and try again:

result = time_machine(5, "years")
print(result)

I wrote my code like this and it worked just fine.

def time_machine(nbr, t_me):
    if t_me == 'years':
        starter += datetime.timedelta(days = nbr * 365)
    elif t_me == 'minutes':
        starter += timedelta(minutes = nbr)
    elif t_me == 'hours':
        starter += datetime.timedelta(hours = nbr)
    return result

Good luck

Robert Raiz
Robert Raiz
2,811 Points

I wrote it without that part too and I get the same thing: https://i.imgsafe.org/646b8be51b.png the print and result part was to check that in the script on the local interpreter.

I think it's a bug or something.

Kent Åsvang
Kent Åsvang
18,823 Points

You need to include "days" as an elif-statement also

Robert Raiz
Robert Raiz
2,811 Points

You were correct. Totally my bad as I did not even consider that "days" might be one of the arguments although it was clearly stated in the requirement... geez. my bad.

Kent Åsvang
Kent Åsvang
18,823 Points

No worries. Stuff like that happens a lot