我已經創建了一個腳本來計算星期幾,但我必須使用太多的if子句。有沒有辦法做到這一點,而不是使用太多的ifs。減少太多,如果子句
實際鏈接
http://pc2solution.blogspot.in/2014/09/python-calculate-day-of-week-for-any.html
腳本:
import datetime
year = raw_input('Enter year in format YYYY... ')
mon = raw_input('Enter month in format MM ')
date = raw_input('Enter date in format ')
a = datetime.date(int(year), int(mon), int(date)).weekday() # 3=Thursday
if a == 0:
print 'Its Monday'
if a == 1:
print 'Its Tuesday'
if a == 2:
print 'Its Wednesday'
if a == 3:
print 'Its Thursday'
if a == 4:
print 'Its Friday'
if a == 5:
print 'Its Saturday'
if a == 6:
print 'Its Sunday'
[Dictionary](https://docs.python.org/2/tutorial/datastructures.html)。 – Maroun 2014-09-02 06:58:36
或者根本不打擾;使用'strftime'和'%A'指令:https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior – jonrsharpe 2014-09-02 06:59:21