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

please help

Create a function named is_over_13 that takes a datetime and returns whether or not the difference between that datetime and today is 4745 days (13 years × 365 days, ignoring leap years) or more.

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(dt): # compare total days delta = today - dt return delta.days >= 4745
def date_string(dt): 
    return dt.strftime("%B %d")
birth_dates = map(date_string, filter(is_over_13, birthdays))

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,998 Points

Nice work! The code you supplied does pass the challenge if it is formatted/spaced properly. Python and (by extension) the challenge automated grader are very picky about indentation. Here is the snippet of code that is the user supplied code which can pass the challenge.

def is_over_13(dt):
    # compare total days
    delta = today - dt
    return delta.days >= 4745

def date_string(dt):
    return dt.strftime("%B %d")

birth_dates = map(date_string, filter(is_over_13, birthdays))
Blue DiamondXD
Blue DiamondXD
5,369 Points

what is delta use again???????

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,998 Points

That is a good question for a new developer to ask.

"Delta" is a term that means difference between one item and another. You sometimes hear "delta change" or just "delta" when programmers are discussing state changes or differences in two states.

In our case, "delta" is the name of a variable that holds the difference between today and the parameter "dt" which is another date. Because "delta" is a datetime object, we can look at the difference in days (or other time units).

Blue DiamondXD
Blue DiamondXD
5,369 Points

so it like something to let the rest of the code to keep in mid where am i in the world right?

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,998 Points

I am not a fan of the way that the code is written above, as I am more of a proponent of "functional" programming that minimizes dependencies on global variables.

In particular for the code presented above, "today" is calculated outside of the is_over_13() function. I would rather have the option to specify what "today" is being considered (could be a future, or past date) or if the parameter were omitted, then the function would calculate today's date.

Here's the code.

import datetime

def is_over_13(dt, today=None):
    """function calculates if a person's age is over 13 years of age"""
    # this code allows us to override the "today" variable, or use today's actual date.
    # compare total days
    if today is None:
        today = datetime.datetime.now()
    delta = today - dt
    return delta.days >= 4745

Here's how we could use it...

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> def is_over_13(dt, today=None):
...     """function calculates if a person's age is over 13 years of age"""
...     # this code allows us to override the "today" variable, or use today's actual date.
...     # compare total days
...     if today is None:
...         today = datetime.datetime.now()
...     delta = today - dt
...     return delta.days >= 4745
...
>>> # let's get today's date for our example.
>>> today = datetime.datetime.now()
>>> today
datetime.datetime(2019, 12, 21, 9, 34, 0, 861000)
>>> # we see that today is 2019 December 21 at 09:34
>>> # Nancy is 12 now, but will turn 13 in 2020
>>> # Nancy's birthday is January 1, 2007
>>> nancy = datetime.datetime(2007,1,1)
>>> is_over_13(nancy)
False
>>> # let's check if Nancy will be 13 this coming summer (2020 Jun 21)
>>> summer_2020 = datetime.datetime(2020,6,21)
>>> is_over_13(nancy, summer_2020)
True
Blue DiamondXD
Blue DiamondXD
5,369 Points

ohhh i get it now tanks !!!