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 Functional Python Functional Workhorses Map and Filter

functional programming 'datetime' confusion

Hi,

So, I am working on step one of the "map/filter" code challenge in the functional programming lesson. It's been a while since I've dealt with the datetime stuff so I am going back and am doing some refreshers. This is all well and good.

The problem I am having is that the 'Bummer' commentary isn't that helpful when it comes to debugging a problem so what I often do is write the code in Idle or Notepad++ in order to get more practice coding as well as getting any pertinent error messages that might help me reach my goal.

Now, I have gone to my Idle editor and typed in exactly what the original code is (minus my function) and when I run it I get this error:

"line4, in <module> datetime.datetime(2014, 4, 29), TypeError: 'module' object is not callable."

Can someone explain what is happening?

Thanks

birthdays.py
import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

today = datetime.datetime.today()

def is_over_13(td):
  return today.days - td.days >= 4745

5 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
def is_over_13(today,dt):
  how_many_days = today - dt
  return how_many_days.days >= 4745

Your function doesn't need to accept today, though. today comes from the global scope. Other than that, your code looks correct to me.

I see. Thanks, that worked. I guess my understanding of scope in this context is lacking...

Dear Kenneth, I have similar code to yours but it doesn't pass.

def is_over_13(today, dt):
    difference = today - dt
    return difference.days >= 4745
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ethan Chae Yeah, read the comment. That code is Adam's and it's wrong. The wrongness is explained in the comment.

That error doesn't make sense unless you named your file datetime.py or something like that.

bingo. Thanks.

As long as I have your attention. I've been working on this for a bit and I think I don't fully comprehend what it is you are asking for. the function takes a datetime but needs to determine the difference between two. Here is my attempt

def is_over_13(today, dt):
  return today.days - dt.days >= 4745

This is clearly not correct. Any advice?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

datetime objects don't have a days attribute. timedelta objects do, though. You need to create the timedelta and then compare the days attribute to the target.

Hmmmm.....

I tried this

def is_over_13(today,dt):
  how_many_days = today - dt
  return how_many_days.days >= 4745

to no avail.

(insert interrobang here)

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

That error doesn't make sense unless you named your file datetime.py or something like that. Importing datetime and calling datetime.datetime() is 100% valid.

i passed with this function after a bit of a struggle

def is_over_13(dt): """Is the difference between datetime and today 4745 days or more""" difference = today - dt return difference.days >= 4745