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

大哥们,我想自己做,不用模块,谢谢各位。重赏

追答

Python的伟大之一在于其有着大量的开放库 -- 而且很好用
calendar 不用, datetime模块也不能用? 用来练习?
考虑一下这个练习会涉及到的几个点:
1. 获取年月
2. 根据年月构造数据
2.1 判断日期的星期
2.2 判断月份的最后一天
3. 展现数据

追问

小弟就是为了练习请大哥为小弟指点迷津,赐予代码,重重有赏

追答

sorry, 既然是练习, 请按上面提到的4个功能点思考, 没有现成代码; 只有具体问题具体分析的探讨 :)
再一点: 为了方便展现, 可以考虑在组织数据的时候用何种结构存储

温馨提示:答案为网友推荐,仅供参考