python万年历实现代码 含运行结果

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现万年历的具体代码,供大家参考,具体内容如下

#coding:utf-8
def leap_year(year):#判断平瑞年
  if year%4==0 and year%100!=0 or year%400==0:
    return True
  else:
    return False
def getMonthDays(year,month):#得到每个年份每月的天数
  days = 31
  if month == 2 :
    if leap_year(year):
      days=29
    else:
      days=28
  elif month==4 or month==6 or month==9 or month==11:
    days=30
  return days

def getTotalDays(year,month):#计算星期
  totalDays=0
  for i in range(1,year):
    if leap_year(i):
      totalDays += 366
    else:
      totalDays += 365
  for i in range(1,month):
    totalDays +=getMonthDays(year,i)
  return totalDays
year=input("输入年份:")
month = input("请输入月:")
iCount = 0
print "日\t一\t二\t三\t四\t五\t六"
i=1
for i in range((getTotalDays(year,month)%7)+1):
    print '\t',
    iCount+=1
for i in range(1,getMonthDays(year,month)+1):
    print i,'\t',
    iCount +=1
    if iCount%7 == 0 :
      print ''

运行效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python求最大连续子数组的和

抛出问题: 求一数组如 l = [0, 1, 2, 3, -4, 5, -6],求该数组的最大连续子数组的和 如结果为[0,1,2,3,-4,5] 的和为7 问题分析: 这个问题很...

Matplotlib绘制雷达图和三维图的示例代码

Matplotlib绘制雷达图和三维图的示例代码

1.雷达图 程序示例 '''1.空白极坐标图''' import matplotlib.pyplot as plt plt.polar() plt.show()...

Python实现计算文件MD5和SHA1的方法示例

本文实例讲述了Python实现计算文件MD5和SHA1的方法。分享给大家供大家参考,具体如下: 不多说,直接源码: #file md5 import sys; import hash...

使用Python生成随机密码的示例分享

生成随机密码这件事情用python来干确实相当的方便,优美的string方法加上choice简直是绝配 make_password.py ###简单几行代码执行即可生成记不住的字符...

Python图像处理PIL各模块详细介绍(推荐)

Python图像处理PIL各模块详细介绍(推荐)

 Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内。如open、save、conver、show…等功能...