Home > other >  Making an advent calendar on Python
Making an advent calendar on Python

Time:11-27

I'm trying to create a python script that prints a different statement on every day of December leading up to Christmas.

Below is what I've tried so far as a test but it doesn't work :/

from datetime import date

today = date.today()

nov_27 = 2022-11-27
nov_28 = 2022-11-28

if today == nov_27:
    print("words")
elif today == nov_28:
    print("no words")

CodePudding user response:

Observe that

nov_27 = 2022-11-27
print(nov_27)

gives output

1984

as 2022-11-27 is treated as arithmetic by python, use datetime.date to create date object instance which you can compare with today e.g.

from datetime import date
today = date.today()
nov_27 = date(2022,11,27)
nov_28 = date(2022,11,28)
if today == nov_27:
    print("today is nov_27")
if today == nov_28:
    print("today is nov_28")

gives (at 2022-11-27) output

today is nov_27

CodePudding user response:

Try it like that:

Code:

from datetime import date

today = date.today()

nov_27 = date(2022, 11, 27)
nov_28 = date(2022, 11, 28)

if today == nov_27:
    print("words")
elif today == nov_28:
    print("no words")

Output:

words
  • Related