当前位置:网站首页>【作业】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
边栏推荐
- STM32 Basics - memory mapping
- [learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- 01 use function to approximate cosine function (15 points)
- 【acwing】789. Range of numbers (binary basis)
- 【剑指Offer】42. 栈的压入、弹出序列
- Some properties of leetcode139 Yang Hui triangle
- Jump to the mobile terminal page or PC terminal page according to the device information
- 大整数类实现阶乘
- . Net configuration system
猜你喜欢
![[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

ORM -- query type, association query

0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
![[ORM framework]](/img/72/13eef38fc14d85978f828584e689a0.png)
[ORM framework]

浅谈日志中的返回格式封装格式处理,异常处理

Several schemes of building hardware communication technology of Internet of things

ISP、IAP、ICP、JTAG、SWD的编程特点

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

The method of word automatically generating directory

The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
随机推荐
【acwing】786. Number k
Study summary of postgraduate entrance examination in July
Study summary of postgraduate entrance examination in October
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
1324:【例6.6】整数区间
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
Differences between MCU and MPU
How to cancel automatic saving of changes in sqlyog database
Slurm资源管理与作业调度系统安装配置
P1223 排队接水/1319:【例6.1】排队接水
Yarn的基础介绍以及job的提交流程
Inno setup packaging and signing Guide
1321:【例6.3】删数问题(Noip1994)
. Net configuration system
[sword finger offer] 42 Stack push in and pop-up sequence
Remote meter reading, switching on and off operation command
C logging method
基于gis三维可视化技术的智慧城市建设
mysql插入数据创建触发器填充uuid字段值
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation