First_Ass_Calendar

本文最后更新于:2025年10月14日 晚上

给定年月,输出该月的竖版日历

Example 3, 2023

M 6 13 20 27

T 7 14 21 28

W 1 8 15 22 29

T 2 9 16 23 30

F 3 10 17 24 31

S 4 11 18 25

S 5 12 19 26

思路重现

python的输出逻辑是横板输出,所以在打印竖版日历,就是计算某天是星期几。

代码实现

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def weekly_calendar(w, m, y):
title = ''
if w == 1:
title = "M"
elif w == 2:
title = "T"
elif w == 3:
title = "W"
elif w == 4:
title = "T"
elif w == 5:
title = "F"
elif w == 6:
title = "S"
elif w == 7:
title = "S"
j = 1
first_day = get_weekday_of_date(1, m, y)
if first_day > w:
print(f"{title}", end=" ")
else:
print(f"{title}", end=" ")

while j < get_maximum_days_in_month(m, y)+1:

if get_weekday_of_date(j, m, y) == w:
if j < 10:
print(j, end=" ")
else:
print(j, end=" ")
j = j+1
print()


def get_maximum_days_in_month(m, y):
if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:
return 31
elif m == 4 or m == 6 or m == 9 or m == 11:
return 30
else:
if is_leap(y):
return 29
else:
return 28


def get_weekday_of_date(d, m, y): # 计算某天是星期几
count_day = get_total_days_in_years(y) + get_total_days_in_months(m, y) + get_day_of_month(d)
return (count_day % 7) + 1


def is_leap(y):
if y % 4 == 0:
if y % 100 == 0:
if y % 400 == 0:
return True
else:
return False
else:
return True
else:
return False


def get_total_days_in_years(y):
i = 1900
count1 = 0
while i != y:
if is_leap(i):
count1 += 366

else:
count1 += 365

i = i + 1
return count1


def get_total_days_in_months(m, y):
i = 1
count2 = 0
while i != m:
count2 += get_maximum_days_in_month(i, y)
i = i +1
return count2


def get_day_of_month(d):
return d-1




def generate_vertical_calendar():
y = int(input("Please choose the year:"))
m = int(input("Please choose the month:"))
i = 1
while i != 8:
weekly_calendar(i, m, y)
i = i + 1


generate_vertical_calendar()