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 trialBenjamin Cottam
3,162 PointsWrong number of Minutes?
The code gives the feedback "Got the wrong number of minutes. Expected 7, got -53> Write a function named minutes that takes two datetimes and returns the number of minutes, rounded, between them. The first will always be older and the second newer. You'll need to subtract the first from the second. Whats up?
import datetime
def minutes(time1, time2):
return round(time2.minute - time1.minute)
3 Answers
akak
29,445 PointsLet's assume time1 = (2015, 9, 18, 21, 32, 34, 989839) and time2 = (2015, 9, 18, 22, 16, 4, 442252). This is how datetime.datetime object looks like. When you do time2.minutes minus time1.minutes it takes 16 (as this is the value of minutes in 2nd object) and subtracts 32 (value of minutes in the first object) which gives -16. Well that's not a correct difference.
But when you do time2-time1 you get timedelta objects that equals (0, 2609, 452413). 0 days, 2609 seconds and 452413 microseconds have passed between those two times. Now if you format it taking seconds and dividing by 60 you get 43 minutes between those two times. Which is correct.
Hope that helps :)
akak
29,445 PointsHi, UPDATE: I read that wrong. But you may try this :)
def minutes(time1, time2):
return round((time2 - time1).seconds/60)
James N
17,864 Pointsthis isn't working for me. when i try the above code, i get :
Traceback (most recent call last):
File "Treehouse.py", line 5, in <module>
print(minutes(datetime.datetime.now().month , datetime.datetime.today()))
File "Treehouse.py", line 3, in minutes
return round((datetime2, datetime1).second/60)
AttributeError: 'tuple' object has no attribute 'second'
if i remove the brackets, surrounding the time2
and time1
variables, i get:
Traceback (most recent call last):
File "Treehouse.py", line 5, in <module>
print(minutes(datetime.datetime.now().month , datetime.datetime.today()))
File "Treehouse.py", line 3, in minutes
return round(datetime2, datetime1.second/60)
AttributeError: 'int' object has no attribute 'second'
Can someone help me here? Thanks.
James N
17,864 PointsEDIT: I just tried copying and pasting your code and it works fine.
my code was:
import datetime
def minutes(datetime1, datetime2):
return round(datetime2, datetime1.second/60)
But i don't understand why you put brackets around time2 and time1.
Can someone explain the Nitty Gritty of it all?
Chris Freeman
Treehouse Moderator 68,441 PointsJames, it looks like you have a typo. There should be a minus sign (-) not a comma in the return
line:
return round((datetime2, datetime1).second/60)
Change to
return round((datetime2 - datetime1).second/60)
James N
17,864 PointsWhoops! Thanks!
Benjamin Cottam
3,162 PointsThanks, Why did that way work and not the code I used?
Chris Freeman
Treehouse Moderator 68,441 PointsBenjamin, your code subtracted the minute values without regard to the difference in the hours. In Akak's solution, subtracting two datetime
objects creates a timedelta
object that has the absolute difference. Then it's a matter of converting the units as needed.