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 trialMokanye Tumelo Tricia
9,981 Pointsplease 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.
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
Treehouse Moderator 28,722 PointsNice 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))
Jeff Muday
Treehouse Moderator 28,722 PointsThat 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).
Kurobe Kuro^T_T^
5,369 Pointsso it like something to let the rest of the code to keep in mid where am i in the world right?
Jeff Muday
Treehouse Moderator 28,722 PointsI 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
Kurobe Kuro^T_T^
5,369 Pointsohhh i get it now tanks !!!
Kurobe Kuro^T_T^
5,369 PointsKurobe Kuro^T_T^
5,369 Pointswhat is delta use again???????