python实现万年历(一行三个月份)

实现效果如下:

 python实现万年历(一行三个月份)_第1张图片

代码如下:

# -*- coding:utf-8 -*-
from datetime import datetime
import string

#判断平闰年
def leap_year(year):
    if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
        return True
    else:
        return False

#二维数组
def two_array(year,month):
    array = [[] for i in range(5)]
    weekday = datetime(year, month, 1).weekday()
    index=1
    amount=getMonthDays(year,month)

    for i in range(0, len(array)):
        if i == 0:
            while weekday != 6:
                for a in range(0, weekday+1):
                    array[i].append(0)
                break
            if len(array[i]) != 7:
                while len(array[i]) != 7:
                    array[i].append(index)
                    index = index + 1
        elif i == 1:
            for a in range(index, index + 7):
                array[i].append(a)
            index = index + 7
        elif i == 2:
            for a in range(index, index + 7):
                array[i].append(a)
            index = index + 7
        elif i == 3:
            for a in range(index, index + 7):
                array[i].append(a)
            index = index + 7
        elif i == 4:
            count = 1
            while index <= amount and count <= 7:
                array[i].append(index)
                index = index + 1
                count = count + 1
    if index <= amount:
        array.append([])
        for i in range(index, amount+1):
            array[len(array) - 1].append(i)
    return array

#得到每个年份每月的天数
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 print_date(array,x):
    for i in range(0,len(array[x])):
        if array[x][i]==0:
            print(" \t", end='')
        elif array[x][i]<10:
            print(" "+'%d'%array[x][i]+"\t",end='')
        else:
            print('%d'%array[x][i]+"\t",end='')
    if len(array[x])!=7:
        for i in range(len(array[x]),7):
            print("  \t",end='')
    print("\t\t",end='')


#主函数
def main(year):
    print("\t\t\t一月\t\t\t\t\t\t\t\t二月\t\t\t\t\t\t\t\t三月")
    print("日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t")
    array1 = two_array(year, 1)
    array2 = two_array(year, 2)
    array3 = two_array(year, 3)
    #第一行
    print_date(array1,0)
    print_date(array2,0)
    print_date(array3,0)

    print("\n")

    #第二行
    print_date(array1, 1)
    print_date(array2, 1)
    print_date(array3, 1)

    print("\n")

    # 第三行
    print_date(array1, 2)
    print_date(array2, 2)
    print_date(array3, 2)
    print("\n")

    # 第四行
    print_date(array1, 3)
    print_date(array2, 3)
    print_date(array3, 3)
    print("\n")

    # 第五行
    add_row1=print_date(array1, 4)
    add_row2=print_date(array2, 4)
    add_row3=print_date(array3, 4)
    print("\n")
    if len(array1)==6 or len(array2)==6 or len(array3)==6:
        if len(array1)==6:
            print_date(array1, 5)
        else:
            print("									",end='')
        if len(array2)==6:
            print_date(array2, 5)
        else:
            print("									",end='')
        if len(array3)==6:
            print_date(array3, 5)
        else:
            print("									",end='')
        print("\n")

    print("\t\t\t四月\t\t\t\t\t\t\t\t五月\t\t\t\t\t\t\t\t六月")
    print(
        "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t")
    array4 = two_array(year, 4)
    array5 = two_array(year, 5)
    array6 = two_array(year, 6)

    # 第一行
    print_date(array4, 0)
    print_date(array5, 0)
    print_date(array6, 0)

    print("\n")

    # 第二行
    print_date(array4, 1)
    print_date(array5, 1)
    print_date(array6, 1)

    print("\n")

    # 第三行
    print_date(array4, 2)
    print_date(array5, 2)
    print_date(array6, 2)
    print("\n")

    # 第四行
    print_date(array4, 3)
    print_date(array5, 3)
    print_date(array6, 3)
    print("\n")

    # 第五行
    add_row1 = print_date(array4, 4)
    add_row2 = print_date(array5, 4)
    add_row3 = print_date(array6, 4)
    print("\n")

    if len(array4)==6 or len(array5)==6 or len(array6)==6:
        if len(array4)==6:
            print_date(array4, 5)
        else:
            print("									",end='')
        if len(array5)==6:
            print_date(array5, 5)
        else:
            print("									",end='')
        if len(array6)==6:
            print_date(array6, 5)
        else:
            print("									",end='')
        print("\n")
    print("\t\t\t七月\t\t\t\t\t\t\t\t八月\t\t\t\t\t\t\t\t九月")
    print(
        "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t")
    array7 = two_array(year, 7)
    array8 = two_array(year, 8)
    array9 = two_array(year, 9)

    # 第一行
    print_date(array7, 0)
    print_date(array8, 0)
    print_date(array9, 0)

    print("\n")

    # 第二行
    print_date(array7, 1)
    print_date(array8, 1)
    print_date(array9, 1)

    print("\n")

    # 第三行
    print_date(array7, 2)
    print_date(array8, 2)
    print_date(array9, 2)
    print("\n")

    # 第四行
    print_date(array7, 3)
    print_date(array8, 3)
    print_date(array9, 3)
    print("\n")

    # 第五行
    add_row1 = print_date(array7, 4)
    add_row2 = print_date(array8, 4)
    add_row3 = print_date(array9, 4)
    print("\n")

    if len(array7) == 6 or len(array8) == 6 or len(array9) == 6:
        if len(array7) == 6:
            print_date(array7, 5)
        else:
            print("									", end='')
        if len(array8) == 6:
            print_date(array8, 5)
        else:
            print("									", end='')
        if len(array9) == 6:
            print_date(array9, 5)
        else:
            print("									", end='')
        print("\n")
    print("\t\t\t十月\t\t\t\t\t\t\t\t十一月\t\t\t\t\t\t\t\t十二月")
    print(
        "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "\t\t" + "日\t" + "一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t")
    array10 = two_array(year, 10)
    array11 = two_array(year, 11)
    array12 = two_array(year, 12)

    # 第一行
    print_date(array10, 0)
    print_date(array11, 0)
    print_date(array12, 0)

    print("\n")

    # 第二行
    print_date(array10, 1)
    print_date(array11, 1)
    print_date(array12, 1)

    print("\n")

    # 第三行
    print_date(array10, 2)
    print_date(array11, 2)
    print_date(array12, 2)
    print("\n")

    # 第四行
    print_date(array10, 3)
    print_date(array11, 3)
    print_date(array12, 3)
    print("\n")

    # 第五行
    add_row1 = print_date(array10, 4)
    add_row2 = print_date(array11, 4)
    add_row3 = print_date(array12, 4)
    print("\n")

    if len(array10) == 6 or len(array11) == 6 or len(array12) == 6:
        if len(array10) == 6:
            print_date(array10, 5)
        else:
            print("									", end='')
        if len(array11) == 6:
            print_date(array11, 5)
        else:
            print("									", end='')
        if len(array12) == 6:
            print_date(array12, 5)
        else:
            print("									", end='')
        print("\n")
year=int(input(("请输入年份:")))

main(year)

 

 

你可能感兴趣的:(python)