• 技术文章 >常见问题 >Python常见问题

    Python怎么打印日历?

     Ly Ly2020-05-25 16:32:24原创4567

    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、运行结果:

    p7.jpg

    专题推荐:python
    上一篇:python如何删除某个目录文件夹? 下一篇:python如何输出2进制数?

    相关文章推荐

    • python什么等价于True?• python怎么过滤掉空行?• python如何取余和取商?• python怎么给list加序号?• python如何去空格和回车?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网