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!

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 Let's Build a Timed Quiz App Harder Time Machine

Stephen Link
Stephen Link
3,685 Points

What are the test values for the time_machine function?

I tested my code before submitting it. I used:

print(time_machine(1, "years"))
print(time_machine(1, "minutes"))
print(time_machine(1, "hours"))
print(time_machine(1, "days"))

and the resulting output was:

2015-10-21 16:29:00
2016-10-20 16:29:00
2015-10-21 16:30:00
2015-10-21 17:29:00
2015-10-22 16:29:00

I get the sort of output that I had expected. The error message on my submission is cryptic. I'm not sure what sort of input produced the undesired output. Since I get the output that I expected, and I don't know what the grader isn't happy with, I don't know how to proceed with troubleshooting. My function is below.

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(number, string):
    """ number is an integer indicating how many units of time to add
    to starter. string indicates what sort of time to add to starter.
    Valid values are minutes, hours, days, or years.
    temp = starter
    if string == "years":
        for _ in range(number):
            temp += datetime.timedelta(days = 365)
    if string == "minutes":
        temp += datetime.timedelta(minutes = number)
    if string == "hours":
        temp += datetime.timedelta(hours = number)
    if string == "days":
        temp += datetime.timedelta(days = number)
    return temp
Stephen Link
Stephen Link
3,685 Points

So I kept hacking away and finally found something which worked. I removed "temp" from my code and now each "if" statement has return starter + datetime.timedelta<arguments>. The grader liked this and I passed. However, as I stated in my question, my original code gave me the results that I expected when I ran it on my machine or in a workspace. What was wrong with the original code?

1 Answer

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That is a bit strange. Here's my best guess:

Since you're doing a loop for adding on days, we might cross a leap year. Then, when it gets to the final datetime, you're off by a day or two. I used exactly your code, but instead of the loop did temp += datetime.timedelta(days=365*number) and it passed just fine.


Actually, we see just that in your example output. Your day has changed when you did years.