Так что я впервые пишу код на Python, и я хотел сделать небольшой проект. Я подумал о том, что хочу сделать, и подумал, что люди могут захотеть узнать, сколько дней до каждого праздника.
Как мне найти разницу между сегодняшней датой и праздниками?
from datetime import datetime, timedelta
now = datetime.now() + timedelta(days = 10)
def get_user_holiday():
holiday = input("What is your favorite holiday? ")
if holiday == "New Year's Day":
new_years_Day = datetime(now.year, 1, 1)
elif holiday == "Martin Luther King Jr. Day":
martin_luther_king_jr_Day = datetime(now.year, 1, 21)
elif holiday == "Groundhog Day":
groundhog_Day = datetime(now.year, 2, 2)
elif holiday == "Valentine's Day":
valentines_Day = datetime(now.year, 2, 14)
elif holiday == "Ash Wednesday":
ash_Wednesday = datetime(now.year, 3, 6)
elif holiday == "St. Patrick's Day":
st_patricks_Day = datetime(now.year, 3, 17)
elif holiday == "April Fool's Day":
april_fools_Day = datetime(now.year, 4, 1)
elif holiday == "Palm Sunday":
palm_Sunday = datetime(now.year, 4, 14)
elif holiday == "Good Friday":
good_Friday = datetime(now.year, 4, 19)
elif holiday == "Easter Day":
easter_Day = datetime(now.year, 4, 21)
elif holiday == "Mother's Day":
mothers_Day = datetime(now.year, 5, 12)
elif holiday == "Memorial Day":
memorial_Day = datetime(now.year, 5, 27)
elif holiday == "Father's Day":
fathers_Day = datetime(now.year, 6, 16)
elif holiday == "Independence Day":
independence_Day = datetime(now.year, 7, 4)
elif holiday == "Labor Day":
labor_Day = datetime(now.year, 9, 2)
elif holiday == "Columbus Day":
columbus_Day = datetime(now.year, 10, 4)
elif holiday == "Halloween Day":
halloween_Day = datetime(now.year, 10, 31)
elif holiday == "Veterans Day":
veterans_Day = datetime(now.year, 11, 11)
elif holiday == "Thanksgiving Day":
thanksgiving_Day = datetime(now.year, 11, 28)
elif holiday == "Hanukkah Day":
hanukkah_Day = datetime(now.year, 12, 22)
elif holiday == "Christmas Day":
christmas_Day = datetime(now.year, 12, 25)
elif holiday == "New Year's Eve Day":
new_years_eve_Day = datetime(now.year, 12, 31)
else:
print("Not a valid holiday. ")
return holiday
Это то, к чему я отредактировал код.
from datetime import datetime, timedelta
now = datetime.now()
def get_user_holiday():
global now
holiday = input("What is your favorite holiday? ")
if holiday == "New Year's Day":
return datetime(now.year, 1, 1)
elif holiday == "Martin Luther King Jr. Day":
return datetime(now.year, 1, 21)
elif holiday == "Groundhog Day":
return datetime(now.year, 2, 2)
elif holiday == "Valentine's Day":
return datetime(now.year, 2, 14)
elif holiday == "Ash Wednesday":
return datetime(now.year, 3, 6)
elif holiday == "St. Patrick's Day":
return datetime(now.year, 3, 17)
elif holiday == "April Fool's Day":
return datetime(now.year, 4, 1)
elif holiday == "Palm Sunday":
return datetime(now.year, 4, 14)
elif holiday == "Good Friday":
return datetime(now.year, 4, 19)
elif holiday == "Easter Day":
return datetime(now.year, 4, 21)
elif holiday == "Mother's Day":
return datetime(now.year, 5, 12)
elif holiday == "Memorial Day":
return datetime(now.year, 5, 27)
elif holiday == "Father's Day":
return datetime(now.year, 6, 16)
elif holiday == "Independence Day":
return datetime(now.year, 7, 4)
elif holiday == "Labor Day":
return datetime(now.year, 9, 2)
elif holiday == "Columbus Day":
return datetime(now.year, 10, 4)
elif holiday == "Halloween Day":
return datetime(now.year, 10, 31)
elif holiday == "Veterans Day":
return datetime(now.year, 11, 11)
elif holiday == "Thanksgiving Day":
return datetime(now.year, 11, 28)
elif holiday == "Hanukkah Day":
return datetime(now.year, 12, 22)
elif holiday == "Christmas Day":
return datetime(now.year, 12, 25)
elif holiday == "New Year's Eve Day":
return datetime(now.year, 12, 31)
else:
print("Not a valid holiday. ")
return holiday
def calculate_dates(holiday, now):
diff = holiday - now
if diff.days >= 0: # holiday is upcoming this year
return diff.days
else: # next holiday occurs next year
return ((holiday + timedelta(years = 1)) - now).days
hd = get_user_holiday()
now = datetime.now()
calc = calculate_dates(hd, now)
print(calc)