一、题目要求与分析

根据输入的年和月,打印该月的日历。如图所示:

Python实例教程之检索输出月份日历表

题目分析:复杂的问题往往很简单,只需要找到拆分点即可,就像这种题可以分为三个函数:

"text-align: center">Python实例教程之检索输出月份日历表

为了方便起见,我们在这里直接把函数的名字给定一下:

"htmlcode">

def day(y, m, d):#计算y年m月d日是星期几

 y0 = y - (14 - m)//12
 x = y0 + y0//4 - y0//100 + y0 //400
 m0 = m + 12*((14 - m)//12) - 2
 d0 = (d + x + 31*m0//12) % 7
 return d0 #注意,周日是0

def isLeapYear(year): #判断year年是否闰年
 isLeapYear = True 
 if year % 4 ==0: #整除可不是等于0,天
 if year % 100 ==0:
 if year % 400 ==0:
 isLeapYear = True
 else:
 isLeapYear = False
 else:
 isLeapYear = True 
 else:
 isLeapYear = False #注意是一个等号还是两个等号
 return isLeapYear

def calendar(y, m): #打印y年m月日历
 print(' {}年{}月'.format(y,m))
 print('Su\tM\tTu\tW\tTh\tF\tSa')
 # 请在下面编写代码
 # ********** Begin ********** #
 month_1 = [1,3,5,7,8,10,12]
 month_2 = [4,6,9,11]
 momth_number = 0
 if isLeapYear(y): #计算这个月有多少天
 if m == 2:
 month_number = 29
 else:
 if m in month_1:
 month_number = 31
 if m in month_2:
 month_number = 30
 else:
 if m == 2:
 month_number = 28
 else:
 if m in month_1:
 month_number = 31
 if m in month_2:
 month_number = 30 
 
 table = day(y, m, 1) #空格 排列输出
 for j in range (1,month_number + 1): #注意加一
 if j == 1:
 print("\t" * table,end = '')
 j = str(j)
 print(j + "\t",end = '')
 j = eval(j)
 if (j + day(y, m, 1)) % 7 == 0:
 print("\r")
 print("\r")
 
for (y,m) in [(2020,12), (2017,2), (2016,2)]:
 calendar(y, m)
 print('---------------------------')

三、我们来逐个fenxi

(1) day(y, m, d)函数

这个函数没有什么实质性的技术含量,因为这里涉及到一个数学的问题,比如,直接用数学公式,下边我提供一下本函数用的三个数学公式:

Python实例教程之检索输出月份日历表

假如给定了y,m,d,则上边的d0就是要求的星期几。

注意:星期日代表的数字是0,切记,但别问我咋知道的。

函数就这样出来了:

def day(y, m, d):
 y0 = y - (14 - m)//12
 x = y0 + y0//4 - y0//100 + y0 //400
 m0 = m + 12*((14 - m)//12) - 2
 d0 = (d + x + 31*m0//12) % 7
 return d0 

(2) disLeapYear(year)函数

关于判断闰年这件事想必很好知晓,这里我提供一张流程结构图仅供参考:

Python实例教程之检索输出月份日历表

"htmlcode">

def isLeapYear(year): #判断year年是否闰年
 isLeapYear = True 
 if year % 4 ==0: #整除可不是等于0,天
 if year % 100 ==0:
  if year % 400 ==0:
  isLeapYear = True
  else:
  isLeapYear = False
 else:
  isLeapYear = True 
 else:
 isLeapYear = False #注意是一个等号还是两个等号
 return isLeapYear

(3) calendar(y, m)函数

Python实例教程之检索输出月份日历表

这里要解决两个问题,也就是本次程序的核心:

"text-align: center">Python实例教程之检索输出月份日历表

1 . 这个月有多少天?

"text-align: center">Python实例教程之检索输出月份日历表

"htmlcode">

 table = day(y, m, 1) #求空格数
 for j in range (1,month_number + 1): #注意加一
 if j == 1:
  print("\t" * table,end = '')
 j = str(j)
 print(j + "\t",end = '')
 j = eval(j)
 if (j + day(y, m, 1)) % 7 == 0:
  print("\r")
 print("\r")

注意:

"text-align: center">Python实例教程之检索输出月份日历表

标签:
python输出月份日历表,python输入年份月份输出日历,python输出年月日

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“Python实例教程之检索输出月份日历表”

暂无“Python实例教程之检索输出月份日历表”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。