当前位置:网站首页>【作业】2022.7.6 写一个自己的cal函数
【作业】2022.7.6 写一个自己的cal函数
2022-07-07 08:18:00 【Sprite.Nym】
在linux中用vim写日历函数,基本功能有:
- 输入月和年,输出日历
- 如果不输入月,但有年,则输出1-12月日历
- 如果都不输入,则输出当前日历
- 如果输入月,但不输入年,则默认今年
vim own_cal.py
import datetime
def first_day(m, y):
# 计算出公式中需要的m, y, c
if m == 1 or m == 2:
m += 12
y -= 1
c = y // 100
y %= 100
# 计算并返回每月第一天是星期几
return (y + y//4 + c//4 - 2 * c + 26 * (m+1) // 10) % 7
def total_days(m, y):
# 判断闰年
result = 1 if (y % 100 != 0 and y % 4 == 0) or (y % 400 == 0) else 0
# 判断天数
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):
# 创建cal列表
cal = ['日', '一', '二', '三', '四', '五', '六']
# 获得日历第一排的空格数
for i in range(first_day):
cal.append(' ')
# 获得剩余的日期
for i in range(1, total_days + 1):
cal.append(f'{
i: <2}')
return cal
def draw_cal(cal):
# 创建计数变量t
t = 0
for i in cal:
# t是7的倍数时换行,其他时候空格
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}年{
m:0>2}月日历')
draw_cal(cal)
if __name__ == '__main__':
m = input('请输入月份: ')
y = input('请输入年份: ')
now_date = datetime.datetime.now()
# 如果年份和月份都为空则输出现在, 如果只有月份空着则输出1-12月
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
边栏推荐
- ISP、IAP、ICP、JTAG、SWD的编程特点
- UnityWebRequest基础使用之下载文本、图片、AB包
- The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
- The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
- IPv4 socket address structure
- Guide de signature du Code Appx
- 深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
- Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
- IIC基本知识
- Study summary of postgraduate entrance examination in September
猜你喜欢

Guide de signature du Code Appx

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

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

Remote meter reading, switching on and off operation command

5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!

1324:【例6.6】整数区间
![P1031 [NOIP2002 提高组] 均分纸牌](/img/ba/6303f54d652fa7aa89440e314f8718.png)
P1031 [NOIP2002 提高组] 均分纸牌

基于gis三维可视化技术的智慧城市建设

对存储过程进行加密和解密(SQL 2008/SQL 2012)

Use of JSON extractor originals in JMeter
随机推荐
2022.7.5DAY597
Guid primary key
Postman interface test VI
1323:【例6.5】活动选择
UnityWebRequest基础使用之下载文本、图片、AB包
STM32中AHB总线_APB2总线_APB1总线这些是什么
STM32产品介绍
Programming features of ISP, IAP, ICP, JTAG and SWD
Leetcode exercise - 113 Path sum II
STM32 ADC and DMA
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
JMeter installation
Serial communication relay Modbus communication host computer debugging software tool project development case
Slurm资源管理与作业调度系统安装配置
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
单片机(MCU)最强科普(万字总结,值得收藏)
P2788 数学1(math1)- 加减算式
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
IPv4套接字地址结构