python打印日历

Posted 蓝色天马

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python打印日历相关的知识,希望对你有一定的参考价值。

    #未优化的代码
1
#输出日历 2 def print_calendar(year,month,date = 1): 3 month_dict = {\'1\':\'January\',\'2\':\'February\',\'3\':\'March\',\'4\':\'April\',\'5\':\'May\',\'6\':\'June\',\'7\':\'July\', 4 \'8\':\'August\',\'9\':\'September\',\'10\':\'October\',\'11\':\'November\',\'12\':\'December\'} 5 6 #数字月份转换为字符串,并判断月份和号数是否合法 7 if month in range(1,13) and date in range(1,31): 8 month_str = str(month) 9 if month_str in month_dict: 10 month_str = month_dict[month_str] 11 else: 12 print(\'月份或号数输入不合法\') 13 return -1 14 15 #头部 16 print(\'%15s%8d\'%(month_str,year)) 17 print(\'-\'*33) 18 print(\'Sun Mon Tue Wed Thu Fri Sat\') 19 20 #得到每月1号是星期几 21 first_day = get_start_day(year,month,1) 22 #得到此月有多少天 23 month_num = days_of_month(year,month) 24 25 each_day = 0 26 #主体 27 for index in range(1,43): 28 29 if index < first_day + 1: 30 print(\' \'*5,end = \'\') 31 else: 32 if (index - 1) % 7 == 0: 33 print(\'\') 34 each_day += 1 35 if each_day > month_num: 36 return False 37 if each_day < 10: 38 if each_day == date: 39 print(\'%-5s\'%(\'--\'),end = \'\') 40 else: 41 print(\' %-4d\'%(each_day),end = \'\') 42 else: 43 if each_day == date: 44 print(\'%-5s\'%(\'--\'),end = \'\') 45 else: 46 print(\'%-5d\'%(each_day),end = \'\') 47 48 49 #输入一个年月日,判断是星期几 50 #需要一个比较标准:2010-1-1是星期五 51 #计算当前距离标准过了多少天(total_days % 7 + 5 -1)%7 52 #先遍历年份,是闰年+366,不是+365 53 #再遍历月份,31,30,29,28 54 def get_start_day(year,month,date): 55 total_days = 0 56 #遍历年份 57 for one_year in range(2010,year): 58 if is_leap_year(one_year): 59 total_days += 366 60 else: 61 total_days += 365 62 #print(total_days) 63 #遍历月份 64 for one_month in range(1,month): 65 total_days += days_of_month(year,one_month) 66 #print(total_days) 67 #加上当月号数,则求得总共过了多少天 68 total_days += date 69 70 #求输入的年月日是星期几 71 day = (total_days % 7 + 5 - 1) % 7 72 73 #print(total_days) 74 #print(day) 75 return day 76 77 #输入一个年份和月份,输出这月有多少天 78 #1,3,5,7,8,10,12--------31天 79 #4,6,9,11 --------------30天 80 #如果是闰年2------------29天 81 #不是闰年 2-------------28天 82 def days_of_month(year,month): 83 days = 0 84 if month in (1,3,5,7,8,10,12): 85 days = 31 86 elif month in (4,6,9,11): 87 days = 30 88 elif is_leap_year(year): 89 days = 29 90 else: 91 days = 28 92 return days 93 94 def is_leap_year(year): 95 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: 96 return True 97 return False 98 99 def main(): 100 print(\'*\'*33) 101 year = int(input(\'请输入年份:\')) 102 month = int(input(\'请输入月份:\')) 103 date = int(input(\'请输入号数:\')) 104 print(\'*\'*33) 105 #某年某月有多少天 106 #days = days_of_month(year,month) 107 #print(\'{}年{}月有{}天\'.format(year,month,days)) 108 #某年某月某日是星期几 109 #day = get_start_day(year,month,date) 110 #print(\'{}年{}月{}日是星期{}\'.format(year,month,date,day)) 111 #打印日历 112 print_calendar(year,month,date) 113 114 #执行 115 main()

 

以上是关于python打印日历的主要内容,如果未能解决你的问题,请参考以下文章

python中如何打印某月日历

python Python:将简单的格式化日历打印到控制台

python打印万年历

打印日历

Python3-笔记-E-004-库-日历calendar

C++实现的一个打印日历程序