python_自定日历

时间:2023-03-08 17:26:05
python_自定日历

>>> from datetime import date
>>> daysOfMonth=[31,28,31,30,31,30,31,31,30,31,30,31]

>>> def myCalendar(year,month):
start=date(year,month,1).timetuple().tm_wday
print('{0} year {1} month'.format(year,month).center(56))
print('t'.join('Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split()))
day =daysOfMonth[month-1]
if month==2:
if year%400==0 or (year%4==0 and year%100!=0):
day +=1
result=[' '*8 for i in range(start+1)]
result+=list(map(lambda d:str(d).ljust(8),range(day+1)))
for i,day in enumerate(result):
if i!=0 and i%7==0:
print()
print(day,end='')
print()

>>> def main(year,month=-1):
if type(year)!=int or year<1000 or year>10000:
print('Year error')
return
if type(month)==int:
if month==-1:
for m in range(1,13):
myCalendar(year,m)
elif month in range(1,13):
myCalendar(year,month)
else:
print('Month error')
return
else:
print('Month error')
return

>>> main(2017)
2017 year 1 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday

0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
2017 year 2 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
2017 year 3 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
2017 year 4 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
2017 year 5 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
2017 year 6 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
2017 year 7 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
2017 year 8 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
2017 year 9 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
2017 year 10 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday

0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
2017 year 11 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
2017 year 12 month
SundaytMondaytTuesdaytWednesdaytThursdaytFridaytSaturday
0 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31