Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Timedelta Minute

Laurens Sandt
Laurens Sandt
3,513 Points

minutes.py

my code is

import datetime

year = datetime.timedelta(days=365)
another_year = datetime.timedelta(weeks=40, days=84, hours=23,
                         minutes=45, seconds=600)


def minutes(year, another_year):

    return round((year.total_seconds()-another_year.total_seconds())/60)

print(minutes(year, another_year))

if you run this in Pycharm I get 5 which is the correct answer

however if give as a solution:

import datetime

def minutes(another_year, year):

    return round((year.total_seconds()-another_year.total_seconds())/60)

I am bummed out

obviously I am going wrong somewhere but where?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're not the first to get tripped up on this question. But the idea here is that they want you to subtract the datetime objects directly. I did it all on one line like this:

import datetime

def minutes(another_year, year):
    return round(((year - another_year).total_seconds()) /60)

This will subtract our datetime objects returning a new datetime object that we can then use our total_seconds method on. Then we divide by 60 and round.

However, if you want a truly superb explanation of this challenge, you should check out this answer by a moderator :smiley:

https://teamtreehouse.com/community/confused-43

Michael Hulet
Michael Hulet
47,881 Points

Glad to see you saw this, too, because your answers are always really solid, and often better than mine. Thanks for helping out in the Community!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael Hulet Are you kidding? I :heart: the Community. They've helped me tons! I try and give back when/where I can. But, no, my answers aren't always really solid :wink:

Michael Hulet
Michael Hulet
47,881 Points

Admittedly, my knowledge of the datetime module is a little fuzzy, so this may or may not be right, but if you look closely, you declared year and another_year in a different order between implementations, but used the same line for the inside of the function. Subtraction is an operation that is dependent on the order of numbers involved, so flipping the declaration order of those parameters will affect the output of the function. If the first one works in PyCharm, make sure the line where you declare the minutes function that you give to Treehouse is exactly the same as the one that's in PyCharm