一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

python如何实现万年历效果

时间:2017-05-22 编辑:简简单单 来源:一聚教程网

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

 

 代码如下复制代码

#coding:utf-8

defleap_year(year):#判断平瑞年

  ifyear%4==0andyear%100!=0oryear%400==0:

    returnTrue

  else:

    returnFalse

defgetMonthDays(year,month):#得到每个年份每月的天数

  days=31

  ifmonth==2:

    ifleap_year(year):

      days=29

    else:

      days=28

  elifmonth==4ormonth==6ormonth==9ormonth==11:

    days=30

  returndays

 

defgetTotalDays(year,month):#计算星期

  totalDays=0

  foriinrange(1,year):

    ifleap_year(i):

      totalDays+=366

    else:

      totalDays+=365

  foriinrange(1,month):

    totalDays+=getMonthDays(year,i)

  returntotalDays

year=input("输入年份:")

month=input("请输入月:")

iCount=0

print"日\t一\t二\t三\t四\t五\t六"

i=1

foriinrange((getTotalDays(year,month)%7)+1):

    print'\t',

    iCount+=1

foriinrange(1,getMonthDays(year,month)+1):

    printi,'\t',

    iCount+=1

    ifiCount%7==0:

      print''

 

运行效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,

热门栏目