0
import datetime

now = datetime.datetime.now()
if now.day == Tuesday :
    print ('yes it is tuesday')
else :
     print ('no')

I try this but I get error in python :

Traceback (most recent call last):
  File "C:/Users/Yarlagadda/Desktop/test2.py", line 4, in <module>
    if now.day == Tuesday :
  NameError: name 'Tuesday' is not defined
  >>> 

I would like to know mistake

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Does this answer your question? [How do I get the day of week given a date?](https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date). You have the [`weekday()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.weekday) method which returns the day of the week as an integer – Tomerikoo Sep 01 '20 at 12:02
  • To get a weekday you should use `datetime.datetime.today().weekday()` which returns a number from 0 to 6. If you dont want to use another library, you can create a tuple with weekdays in English like `weekDays = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")` and access it as `weekDays[datetime.datetime.today().weekday()]` – Adil Shirinov Sep 01 '20 at 12:08

2 Answers2

2

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')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
  • @Tomerikoo you're totally right. When I was testing this locally I was using `datetime.weekday()` and just didn't notice I changed it to `.day` in here. I'll update the code. – Rashid 'Lee' Ibrahim Sep 01 '20 at 13:03
-1

The error is just in if condition that the Tuesday is not used as string. Replace Tuesday by 'Tuesday' and your code is working fine.

For enhancement you may also use list or tuple to keep the weekdays.

import datetime
now = datetime.datetime.now()

if now.day == 'Tuesday':
   print ('yes it is tuesday')
else:
   print ('no')
  • While this solves the **error**, it does not solve the **problem**... [`datetime.day`](https://docs.python.org/3/library/datetime.html#datetime.datetime.day) returns an int, so the above code will forever print `"no"`... – Tomerikoo Sep 01 '20 at 12:55