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 trialHolden Glass
6,077 PointsCode works in workspaces, not in challenge
I have run the code in workspaces multiple times and it works. I put it in the challenge and it no longer works. all it says is "Bummer! Try again!" I don't know what to do. any help will be appreciated.
import datetime
def minutes(date1, date2):
seconds1 = date1.total_seconds()
seconds2 = date2.total_seconds()
difference = seconds2 - seconds1
seconds = difference/60
return round(seconds)
2 Answers
Steven Parker
231,269 PointsBe careful about testing a challenge in workspaces or an external REPL.
If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.
I'm guessing that your code seemed to work because you were passing it timedelta arguments, but the challenge says the function should receive datetime arguments. You'll need to make a few changes to handle the correct type of argument.
Hint: Remember that subtracting one datetime from another will produce a timedelta.
Nafeez Quraishi
12,416 PointsBelow worked for me
from datetime import timedelta
def minutes(time_one, time_two):
difference = timedelta.total_seconds(time_two - time_one)
delta_time = round(difference/60)
return delta_time
Holden Glass
6,077 PointsHolden Glass
6,077 PointsOk. Thanks. This will be very helpful for future challenges. Thanks.
Holden Glass
6,077 PointsHolden Glass
6,077 Pointsalso, quick question, do you know of a way to turn a datetime into a timedelta?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI know a way to turn two datetimes into a timedelta (see my hint above).
But it doesn't make sense conceptually to convert just one - in what situation would you want to do that?
Holden Glass
6,077 PointsHolden Glass
6,077 PointsI don't exactly know. I was thinking that I would turn both datetimes into timedeltas and then subtract and do the total_seconds.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou have the right idea but the wrong order. What I was hinting at before is that if you subtract the first datetime from the second one, the result is a timedelta. Then you can take the total_seconds of that.
Holden Glass
6,077 PointsHolden Glass
6,077 PointsI realized that after I completed the challenge another way.