❶ 想用python做個輸入年、月,顯示當年當月日歷的小程序,本人菜鳥,請教各位前輩。重重有賞
import datetime
import calendar
def getYM():
''' 這是一個簡單的年月輸入方法 '''
year = raw_input('Input Year: ')
month = raw_input('Input Month: ')
return year, month
def saveGetYM():
''' 這是一個安全的年月輸入方法 '''
while True:
try:
year_month = raw_input('Input year and month (year,mont): ')
year, month = year_month.split(',')
year, month = int(year), int(month)
if 1900<=year<=2200 and 1<=month<=12:
break
except:
continue
return year, month
year,month = saveGetYM()
c = calendar.Calendar(1)
print '-- %d --'%year
for w in c.monthdatescalendar(year,month)[:7:]:
print '|'.join([d.strftime('%m-%d') for d in w])