当前位置:网站首页>[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
边栏推荐
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- Download Text, pictures and ab packages used by unitywebrequest Foundation
- 【二开】【JeecgBoot】修改分页参数
- OpenGL glLightfv 函数的应用以及光源的相关知识
- IIC基本知识
- 宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
- ORM -- grouping query, aggregation query, query set queryset object properties
- Serial communication relay Modbus communication host computer debugging software tool project development case
- 求方程ax^2+bx+c=0的根(C语言)
- Yarn的基础介绍以及job的提交流程
猜你喜欢
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

Encrypt and decrypt stored procedures (SQL 2008/sql 2012)

OpenGL glLightfv 函数的应用以及光源的相关知识

XML configuration file parsing and modeling

Smart city construction based on GIS 3D visualization technology

SQLyog数据库怎么取消自动保存更改

1321:【例6.3】删数问题(Noip1994)

Postman interface test III

Use the fetch statement to obtain the repetition of the last row of cursor data

深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
随机推荐
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
Appx代码签名指南
P1031 [NOIP2002 提高组] 均分纸牌
ORM model -- creation and query of data records
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
Study summary of postgraduate entrance examination in August
Postman interface test V
Experience sharing of software designers preparing for exams
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Inno setup packaging and signing Guide
table宽度比tbody宽度大4px
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
Postman interface test VII
. Net configuration system
Fiddler break point
Use of JSON extractor originals in JMeter
A small problem of bit field and symbol expansion
Hdu-2196 tree DP learning notes
Jump to the mobile terminal page or PC terminal page according to the device information