您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下Python打印日历的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!
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、运行结果:
看完了这篇文章,相信你对Python打印日历的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。