当前位置:网站首页>[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
边栏推荐
- Postman interface test V
- Talking about the return format in the log, encapsulation format handling, exception handling
- Learning records - high precision addition and multiplication
- Socket通信原理和实践
- [second on] [jeecgboot] modify paging parameters
- Appx代码签名指南
- JMeter installation
- Study summary of postgraduate entrance examination in November
- Word自动生成目录的方法
- MCU与MPU的区别
猜你喜欢
电表远程抄表拉合闸操作命令指令
基于gis三维可视化技术的智慧城市建设
Word自动生成目录的方法
【二开】【JeecgBoot】修改分页参数
Some properties of leetcode139 Yang Hui triangle
The request object parses the request body and request header parameters
String formatting
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
Smart city construction based on GIS 3D visualization technology
P1223 排队接水/1319:【例6.1】排队接水
随机推荐
P1031 [NOIP2002 提高组] 均分纸牌
浅谈日志中的返回格式封装格式处理,异常处理
Adb 实用命令(网络包、日志、调优相关)
PDF文档签名指南
ArcGIS operation: batch modify attribute table
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
2022.7.3DAY595
【作业】2022.7.6 写一个自己的cal函数
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
搭建物联网硬件通信技术几种方案
.NET配置系统
JMeter installation
Use of JSON extractor originals in JMeter
【二开】【JeecgBoot】修改分页参数
01 use function to approximate cosine function (15 points)
Factorial implementation of large integer classes
2022.7.5DAY597
C#记录日志方法
How to cancel automatic saving of changes in sqlyog database
Postman interface test III