当前位置:网站首页>[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
边栏推荐
- 基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
- Pdf document signature Guide
- Study summary of postgraduate entrance examination in October
- 根据设备信息进行页面跳转至移动端页面或者PC端页面
- The request object parses the request body and request header parameters
- 【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
- 每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
- Appx代码签名指南
- Review of the losers in the postgraduate entrance examination
- Study summary of postgraduate entrance examination in July
猜你喜欢

JMeter about setting thread group and time

Methods of adding centerlines and centerlines in SolidWorks drawings

【acwing】786. Number k

Postman interface test III

Postman interface test IV

Programming features of ISP, IAP, ICP, JTAG and SWD

php \n 换行无法输出

String formatting

使用U2-Net深层网络实现——证件照生成程序
![[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program](/img/03/41bb3870b9a6c2ee66099abac08eb3.png)
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
随机推荐
table宽度比tbody宽度大4px
Word自动生成目录的方法
Kotlin实现微信界面切换(Fragment练习)
. Net configuration system
P1223 排队接水/1319:【例6.1】排队接水
Study summary of postgraduate entrance examination in October
Embedded background - chip
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
Fiddler simulates the interface test
使用U2-Net深层网络实现——证件照生成程序
Can I open a stock trading account online? Is it safe
Use of JSON extractor originals in JMeter
根据设备信息进行页面跳转至移动端页面或者PC端页面
Review of the losers in the postgraduate entrance examination
ArcGIS operation: converting DWG data to SHP data
P2788 数学1(math1)- 加减算式
Appx代码签名指南
MCU与MPU的区别
【华为机试真题详解】高矮个子排队
The width of table is 4PX larger than that of tbody