当前位置:网站首页>[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 38 Arrangement of strings [no description written]
- Guid主键
- 1321:【例6.3】删数问题(Noip1994)
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- The mobile terminal automatically adjusts the page content and font size by setting rem
- P2788 数学1(math1)- 加减算式
- 施努卡:机器视觉定位技术 机器视觉定位原理
- 深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
- High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
- 对存储过程进行加密和解密(SQL 2008/SQL 2012)
猜你喜欢

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

ArcGIS operation: batch modify attribute table

1324:【例6.6】整数区间

Appx代码签名指南

Postman interface test II

使用U2-Net深层网络实现——证件照生成程序
![[second on] [jeecgboot] modify paging parameters](/img/59/55313e3e0cf6a1f7f6b03665e77789.png)
[second on] [jeecgboot] modify paging parameters

成为优秀的TS体操高手 之 TS 类型体操前置知识储备

01 use function to approximate cosine function (15 points)

Methods of adding centerlines and centerlines in SolidWorks drawings
随机推荐
Postman interface test IV
施努卡:机器人视觉抓取工作原理 机器视觉抓取
STM32 Basics - memory mapping
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
[email protected] can help us get the log object quickly
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
OpenGL glLightfv 函数的应用以及光源的相关知识
EasyExcel读取写入简单使用
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
SolidWorks工程图中添加中心线和中心符号线的办法
嵌入式背景知识-芯片
leetcode-304:二维区域和检索 - 矩阵不可变
Study summary of postgraduate entrance examination in September
IIC基本知识
The width of table is 4PX larger than that of tbody
Appx code signing Guide
Mongodb creates an implicit database as an exercise
How embedded engineers improve work efficiency
对word2vec的一些浅层理解