[Tutor] What is day of week from either 20180211 or 02112018

Ken Green beachkidken at gmail.com
Tue Feb 6 14:45:49 EST 2018


Greeting: I have been trying to determine the day of
the week when inputting year + month + date. I have
not yet been able to determine what is really needed
for datetime and later on the date in the program below.
Running Python 2.7 on Ubuntu 16.04. Thanks.
===========================================
# A_Weekday.py
from datetime import date
year = "2018"
monthdate = raw_input ("Enter the month and date (MDD or MMDD): ")
print
if (len(monthdate)) == 3:
     month = monthdate[0:1]
     month = "0" + month
     day  = monthdate[1:3]
else:
     month = monthdate[0:2]
     day  = monthdate[2:4]
print month, day, year; print
print year, month, day; print
datecode = year + month + day
print datecode
print
answer = datetime.date(year, month, day).weekday()
print answer
==================================================
Error message below:
Enter the month and date (MDD or MMDD): 211
02 11 2018
2018 02 11
20180211
Traceback (most recent call last):
   File "A_Weekday.py", line 20, in <module>
     answer = datetime.date(year, month, day).weekday()
NameError: name 'datetime' is not defined



More information about the Tutor mailing list