❶ 想用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])