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) Dates and Times Timedelta Minute

Michael Pastran
Michael Pastran
4,727 Points

datetime Challenge

i have no idea how this is working. i just know that it is. lol can anyone explain why? thank you

minutes.py
import datetime

def minutes(datetime1,datetime2):
  return round((datetime1.minute - datetime2.minute)/8)

2 Answers

Hi Michael,

I'm only guessing here because I don't have access to the code challenge backend but I suspect it's a coincidence. I think the code challenge is only running one test and your calculation happened to produce the right answer. The right answer for the wrong reasons.

If you run your code without the divide by 8 you'll get the following error message: 'Got the wrong number of minutes. Expected 7, got 53.'

From this we can deduce that the 2 datetime's are 7 minutes apart and furthermore we know they are crossing an hour boundary.

It's not something like 2:10pm and 2:17pm. It's more like 2:58pm and 3:05pm.

If you were to grab the minutes from each of those and subtract you would get 58 - 5 = 53

This is what the challenge got. It might not be those exact times but I think they're crossing an hour boundary like that.

Going back to your code: 53 / 8 = 6.625 which gets rounded up to 7 and this is the number the challenge is expecting to get.

So you found a calculation that just happened to produce the right number.

This of course is my guess but it seems plausible.

Were you just experimenting and you know how to pass the challenge correctly or do you need hints on how to do it?

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

That's too funny!!! It works because the grader only uses one set of test values that you've gamed to give you the correct answer!

It "works" because datetime1.minute is 59, datetime2.minute is 6. The difference is 53 when divided by 8 is 6.625. Rounded this becomes 7. What you are not checking is that datetime1.hour is 12, datetime2.hour is 13. Remember "older" times have "lower" values.

You're supposed to solve it by realizing that the difference spans across the hour boundary (hint 7+53=60) and may even span the days or large boundaries. It would be best to convert both times to seconds, subtract, then convert results back to minutes.