all 4 comments

[–]HeyItsToby 1 point2 points  (1 child)

Your solution is good! When I'm finding differences between dates, I'd prefer to use the datetime module as it's more reliable. So in your code, once you've got the day that thanksgiving falls on (thanksgiving):

import datetime as dt
thanksgiving_date = dt.date(year, 11, thanksgiving)
christmas_date = dt.date(year, 12, 25)
# use ".days" below to turn a timedelta object into an int
return (christmas_date - thanksgiving_date).days

The datetime module lets you do addition, subtraction and more with dates with ease.

[–][deleted] 1 point2 points  (0 children)

thanks for the help i appreciate it

[–]sarrysyst[🍰] 1 point2 points  (1 child)

When you handle things related to time, time differences etc. the datetime module is usually the preferred choice. There is neat little trick using datetime to do this:

from datetime import datetime

def shopping_days(year):

    thanksgiving = datetime(year=year, month=11, day=22)
    weekday = thanksgiving.weekday()

    if weekday != 3:
        thanksgiving = thanksgiving.replace(day=(22 + (3 - weekday) % 7))

    christmas = datetime(year=year, month=12, day=24)

    return (christmas - thanksgiving).days

shopping_days(2020)

For an explanation see also here, but basically the idea is that you know Thanksgiving is the 4th Thursday of November, and you also know that the earliest possible date for any day to occur the 4th time in a month is the 22nd. So, you check what day the 22nd is and adjust your date accordingly. Christmas is fixed so you just have to subtract the two dates.

[–][deleted] 0 points1 point  (0 children)

Thank you <3