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 trialTJ Takei
13,362 PointsMay I have test cases of time_machines()? I got "Bummer! ..." failure although my test cases work fine.
Dates and Times, Stage 2, Let' Build a Timed Quiz App, time_machine.py:
In [4]: time_machine(1,'years') Out[3]: datetime.datetime(2016, 10, 21, 16, 29)
In [4]: time_machine(1,'hours') Out[4]: datetime.datetime(2015, 10, 21, 17, 29)
In [5]: time_machine(1,'minutes') Out[5]: datetime.datetime(2015, 10, 21, 16, 30)
In [6]: time_machine(1,'days') Out[6]: datetime.datetime(2015, 10, 22, 16, 29)
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
# Remember, you can't set "years" on a timedelta!
def time_machine(n, s):
if s=='minutes': return starter + datetime.timedelta(minutes=n)
if s=='hours': return starter + datetime.timedelta(hours=n)
if s=='days': return starter + datetime.timedelta(days=n)
# s=='years':
return starter.replace(year=starter.year+n)
2 Answers
Kenneth Love
Treehouse Guest TeacherYou don't need to replace()
. Consider a year to be 365 days. Now do you have an idea on how to fix it? If you can't set years, how do you figure out how many days?
Kenneth Love
Treehouse Guest TeacherI've added a note about the years being 365 days. This actually does take into account leap years except for on the years one. .replace()
never takes leap years into account and just blindly replaces values so long as they're valid for that type of time.
TJ Takei
13,362 PointsTJ Takei
13,362 PointsWell, it's an easy going to use the simple minded assumption "1 year == 365 days", and with that I passed the test now. Thanks. But, I think it is also important to take leap years in considerations. The "replace()" is a good workaround for leap year computation. We should see the difference if we had test cases e.g. time_machine(4,'years'), time_machine(100,'years'), or time_machine(400,'years'). I wish the problem had better been clear on this issue.