当前位置:网站首页>[homework] 2022.7.6 write your own cal function
[homework] 2022.7.6 write your own cal function
2022-07-07 10:27:00 【Sprite. Nym】
stay linux of use vim Write calendar function , The basic functions are :
- Enter month and year , Output calendar
- If you do not enter the month , But there are years , The output 1-12 Monthly calendar
- If you do not enter , Then output the current calendar
- If you enter month , But do not enter the year , It defaults to this year
vim own_cal.py
import datetime
def first_day(m, y):
# Calculate what is needed in the formula m, y, c
if m == 1 or m == 2:
m += 12
y -= 1
c = y // 100
y %= 100
# Calculate and return the day of the week on the first day of each month
return (y + y//4 + c//4 - 2 * c + 26 * (m+1) // 10) % 7
def total_days(m, y):
# Judgement of leap year
result = 1 if (y % 100 != 0 and y % 4 == 0) or (y % 400 == 0) else 0
# Judge the number of days
return 31 if m in [1, 3, 5, 7, 8, 10, 12] else (
result + 28 if m == 2 else 30
)
def get_cal(first_day, total_days):
# establish cal list
cal = [' Japan ', ' One ', ' Two ', ' 3、 ... and ', ' Four ', ' 5、 ... and ', ' 6、 ... and ']
# Get the number of spaces in the first row of the calendar
for i in range(first_day):
cal.append(' ')
# Get the remaining dates
for i in range(1, total_days + 1):
cal.append(f'{
i: <2}')
return cal
def draw_cal(cal):
# Create count variables t
t = 0
for i in cal:
# t yes 7 Line feed when multiple of , Other times spaces
print(f'\n{
i}', end='') if t % 7 == 0 else print(f' {
i}', end='')
t += 1
print('\n')
def cal_main(m, y):
y = int(y)
f_day = first_day(m, y)
t_days = total_days(m, y)
cal = get_cal(f_day, t_days)
print(f'{
y: >7} year {
m:0>2} Monthly calendar ')
draw_cal(cal)
if __name__ == '__main__':
m = input(' Please enter the month : ')
y = input(' Please enter the year : ')
now_date = datetime.datetime.now()
# If the year and month are empty, the output is now , If only the month is empty, output 1-12 month
if m == '':
if y == '':
m_list = [now_date.month]
y = now_date.year
else:
m_list = range(1, 13)
else:
m_list = [int(m)]
for m in m_list:
cal_main(m, y=now_date.year)
shift + :wq
边栏推荐
猜你喜欢
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?

【acwing】786. Number k

Experience sharing of software designers preparing for exams

The request object parses the request body and request header parameters

使用U2-Net深层网络实现——证件照生成程序
![[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树](/img/ba/b2dfbf121798757c7b9fba4811221b.png)
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树

基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS

555电路详解

JMeter installation
随机推荐
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Fiddler simulates the interface test
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
table宽度比tbody宽度大4px
2022.7.4DAY596
Study summary of postgraduate entrance examination in July
1321:【例6.3】删数问题(Noip1994)
Programming features of ISP, IAP, ICP, JTAG and SWD
555电路详解
Postman interface test III
Serial communication relay Modbus communication host computer debugging software tool project development case
2022.7.6DAY598
嵌入式背景知识-芯片
PDF文档签名指南
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
Vs code specifies the extension installation location
The width of table is 4PX larger than that of tbody
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
浅谈日志中的返回格式封装格式处理,异常处理