#!/usr/bin/python # -*- coding: UTF-8 -*- __author__ = u'ShelleyZhang' class data: month_dict = {} year = 0 month = 0 def __init__(self,year,month,month_dict): self.year = year self.month = month self.month_dict = month_dict def input(self): try: self.year = int(raw_input(u"Please input target year(1900-3000有效):")) self.month = int(raw_input(u"Please input target month(1-12有效):")) except ValueError: print u"数据格式输入错误" print u"系统默认如下" ''' 判断输入是否正确 ''' def is_input_right(self): if 1900 <= self.year <= 3000 and 1 <= self.month <= 12 and self.month in self.month_dict.keys(): return True else: print u"年月份输入错误" ''' 判断Year是否是闰年 ''' def is_leap_year(self): if self.year % 4 == 0 and self.year % 100 != 0 or self.year % 400 == 0: return True # 是闰年 else: return False # 不是闰年 ''' 返回给定年数的天数 ''' def back_year_num(self): if self.month in (1, 3, 5, 7, 8, 10, 12): # 返回的是31天 return 31 elif self.month in (4, 6, 9, 11): return 30 elif self.is_leap_year(): return 29 else: return 28 ''' 自给定年数(年数可变)到现在经过多少天,本例1900年 ''' def get_mun_day(self): day = 0 for y in range(1900, self.year): if self.is_leap_year(): day += 366 else: day += 365 for m in range(1, self.month): day += self.back_year_num() return day ''' 1900年是星期一,返回的是每个月一号的星期数 ''' def back_month_day(self): return 1 + self.get_mun_day() % 7 def back_month_dict(self): if self.month in self.month_dict: return self.month_dict[self.month] else: print "输入错误" ''' 日历头的输出 ''' def title_month_print(self): if self.is_input_right(): print " ", self.back_month_dict(), " ", self.year print u"*******************************************************" print u" M0 Tu We Th Fr Sa Su " ''' 输出日历的程序 ''' def body_month_print(self): if self.is_input_right(): i = self.back_month_day() #i返回的是指定年月一号的星期数 print " "*(i-1), for j in range(1,self.back_year_num()+1): print "%6d" %j, #输出固定的格式 i += 1 if (i-1)%7 == 0: #每一周结束换行 print "\n" ''' 主函数 ''' month_dict = {1:"January",2:"February",3:"March",4:"April",5:"May", 6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"} da = data(1900,1,month_dict) da.input() da.title_month_print() da.body_month_print()
python日历小程序
最新推荐文章于 2024-08-11 07:30:00 发布