Use the weekday method to find out the day of the week in python's datetime module. The weekday method returns the day of the week of the date created by the object as a number 0-6. Since 0 is Monday, 1 is Tuesday, and 6 is Sunday, you can convert it to a day of the week if you prepare a list that stores Monday to Sunday.
python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from datetime import *
from time import *
print "Enter the date in the following format Example:'2016/07/30'."
user_input_date = raw_input("date:")
yobi = ["Month","fire","water","wood","Money","soil","Day"]
while user_input_date != "bye":
try:
a = datetime.strptime(user_input_data,'%Y/%m/%d')
print "{}Is{}It's the day of the week".format(user_input_date,yobi[a.weekday()])
except ValueError:
print "Wrong date"
user_input_date = raw_input("your date :")
else:
sys.exit(1)
It looks like this when written in python3.x.
python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from datetime import *
from time import *
print ("Please enter the date'2016/07/30'.")
user_input_date = input("your date :")
yobi = ["Month","fire","water","wood","Money","soil","Day"]
while user_input_date != "bye":
try:
a = datetime.strptime(user_input_date,'%Y/%m/%d')
print ("{}Is{}It's the day of the week".format(user_input_date,yobi[a.weekday()]))
except ValueError:
print ("Wrong date")
user_input_date = input("your date :")
else:
sys.exit(1)
Recommended Posts