欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)的簡單萬年歷例子分享

 更新時間:2014年04月25日 11:32:07   作者:  
這篇文章主要介紹了Python實現(xiàn)的簡單萬年歷例子分享,需要的朋友可以參考下

復制代碼 代碼如下:
#!/usr/bin/env python2
#-*- coding:utf-8 -*-
__author__ = 'jalright'

"""
使用python實現(xiàn)萬年歷
"""

def is_leap_year(year):
    """
判斷是否是閏年,返回boolean值
    """
    if year/4==0 and  year/400 !=0:
        return True
    elif year/100 == 0 and year/400 ==0 :
        return True
    else:
        return False

def getMonthDays(year,month):
    """
獲取指定年月的月份有多少天
    """
    days = 31        #31天居多,設置為默認值
    if month == 2 :    #2月份要判斷是否是閏年
        if is_leap_year(year):
            days=29
        else:
            days=28;
    elif month in [4,6,9,11]:     #判斷小月,只有30天
        days=30
    return days

def getTotalDays(year,month):
    """
獲取1990-01-01離現(xiàn)在有多少天,1990-01-01是星期一,以這個為標準來判斷星期
    """
    totalDays=0
    for i in range(1990,year):     #使用range來循環(huán),算出多少年多少天
        if is_leap_year(i):        #判斷是否是閏年
            totalDays += 366
        else:
            totalDays += 365
    for i in range(1,month):       #使用range循環(huán),算出今年前面幾個月過了多少天
        totalDays +=getMonthDays(year,i)
    return totalDays


if __name__ == '__main__':
    while True:                                 #循環(huán)判斷是否輸入錯誤的格式
        print "××××××××××python實現(xiàn)萬年歷××××××××"
        year = raw_input("請輸入年份(如:1990):")
        month = raw_input("請輸入月份:如:1")
        try:                                    #捕捉輸入異常格式和月份的正確
            year = int(year)
            month = int(month)
            if month <1 or month >1:            #判斷月份是否輸入錯誤,錯誤就重新開始循環(huán)
                print "年份或者月份輸入錯誤,請重新輸入!"
                continue
        except:                                 #捕捉到轉換成整型異常,輸出提示,重新開始循環(huán)
            print "年份或者月份輸入錯誤,請重新輸入!"   
            continue
        break     #如果沒有異常就跳出循環(huán)
    #if is_leap_year(year):
    #    print "%s是潤年"%year
    #else:
    #    print "%s是平年"%year
    #print "%s月份總共有%s天!"%(month,getMonthDays(year,month))

    print "日\t一\t二\t三\t四\t五\t六"
    iCount = 0      #計數(shù)器來判斷是否換行
    for i in range(getTotalDays(year,month)%7):
        print '\t',                 #輸出空不換行
        iCount+=1
    for i in range(1,getMonthDays(year,month)):
        print i,
        print '\t',
        iCount +=1
        if iCount%7 == 0 :           #計數(shù)器取余為0,換行
            print ''

相關文章

最新評論