You have two problems with your code.
The first is that the error code is telling you that Tuesday
doesn't exist as a variable because you are not making it a string. So change it to 'Tuesday'
with the '
around it.
Second is that now.day
is going to return a number and not a string day.
Edit: Thanks to @Tomerikoo for pointing out that this returns the day of the month and not the day of the week. Change this to now.weekday()
to get the day of the week.
To fix this, you can either compare the number 1 in the if
statement instead of the word Tuesday or you can map the numbers to the words. Mapping the numbers to the words is the preferred method since it is easier to maintain and less messy.
Note: The days of the week in now.weekday()
will be numbered 0-6 with 0 being Monday and 6 being Sunday.
I'm using a tuple here, but you could use a list, or if you really feel inclined, a dictionary. But a dict is overkill and a list comes with memory overhead that's not needed.
Mapping:
week_days = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
Final Code
import datetime
week_days = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
now = datetime.datetime.now()
day = week_days[now.weekday()] # Note the change to now.weekday()
if day == 'Tuesday' :
print ('yes it is tuesday')
else :
print ('no')