您的当前位置:首页正文

Python怎么打印日历?

2024-08-01 来源:伴沃教育

1、Python打印日历的具体代码:

Lunar=(1,3,5,7,8,10,12)
def IsleapYear(year):
    flag =False
    if(year%4==0 and year%100!=0) or year%400==0:
        flag = True
    return flag
def calculation(year,month):
    sum = 0
    s_year=1990
    while s_year < year-1:
        s_year +=1
        if IsleapYear(s_year):
            sum+=366
        else:
            sum+=365
    s_month=1
    while s_month<month:
        if s_month in Lunar:
            sum += 31
        elif s_month==2:
            if IsleapYear(year):
                sum+=29
            else:
                sum+=28
        else:
            sum+=30
        s_month+=1
    return sum
def display(sum,year,month):
    week=(sum+1)%7
    if month in Lunar:
        day=31
    elif month==2:
        if IsleapYear(year):
            day=29
        else:
            day=28
    else:
        day=30
    print("日\t一\t二\t三\t四\t五\t六")
    count = 0
    space = 0
    while space<=week:
        space+=1
        count+=1
        print("\t",end="")
        if count%7==0:
            print("\n",end="")
    days=1
    while days<=day:
        print(days,"\t",end="")
        days+=1
        count+=1
        if count %7 ==0:
            print("\n")
def main():
    year =int( input("year="))
    if year<1990 or year>9999:
        print("year输入错误")
        main()
    month = int(input("month="))
    if month<1 or month > 12:
        print("month输入错误")
        main()
    sums=calculation(year,month)
    display(sums,year,month)
if __name__=="__main__":
    while True:
        main()
        choose=input("\n是否继续:")
        if choose in("n","N"):
            break

2、运行结果:

显示全文