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个功能点思考, 没有现成代码; 只有具体问题具体分析的探讨 :)
再一点: 为了方便展现, 可以考虑在组织数据的时候用何种结构存储