利用Python函数实现一个万历表完整示例
作者:做个不一样的小丑 时间:2022-06-30 18:06:22
前言
大家可以根据格式化打印字符去调一下最后的输出,不过有中文好像不好调整,可以换成星期的单词,这样应该会好一点,format()函数可以用来格式化打印字符,format()可以使用字符串去调用,也可以独自使用。
可以点进格式化打印字符了解一下哦
示例代码
# 判断是否闰年
def isleap(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
# 判断月的天数
def month_days(year,month):
if month in [1,3,5,7,8,10,12]:
return 31
if month == 2:
if isleap(year):
return 29
else:
return 28
return 30
# 1900年到输入年份的总天数
def total_days(year):
s = 0
for i in range(1900,year):
if isleap(i):
s += 366
else:
s += 365
return s
# 1月到输入月份的天数
def days(year,month):
s = 0
for i in range(1,month):
s += month_days(year,i)
return s
# 获取某年某月的日历
def monthcalendar(year,month):
total = total_days(year) + days(year, month)
a = total % 7
print('星期日'.center(8, ' '), end='')
print('星期一'.center(8, ' '), end='')
print('星期二'.center(8, ' '), end='')
print('星期三'.center(8, ' '), end='')
print('星期四'.center(8, ' '), end='')
print('星期五'.center(8, ' '), end='')
print('星期六'.center(8, ' '), end='')
print()
count = 0
for i in range(0, month_days(year, month) + a + 1):
if i <= a:
print(format(' ','10'), end='')
count += 1
else:
print(format(str(i - a),'^10'), end='')
count += 1
if count == 7:
count = 0
print()
print()
# 输出某年一年的日历
def yearcalendar(year):
for i in range(1,13):
print(f'{i}月:')
monthcalendar(year,i)
print()
# 开始函数
def start():
while True:
print('-------欢迎来到万历表查询页面-------')
print('1.查询某年的日历\n2.查询某年某月的日历\n3.退出查询')
print('---------------------------------')
n = int(input('请输入你的操作:'))
if n == 1:
year = int(input('请输入要查询的年份:'))
yearcalendar(year)
elif n == 2:
year = int(input('请输入要查询的年份:'))
month = int(input('请输入1-12:'))
monthcalendar(year,month)
elif n == 3:
print('退出成功')
break
else:
print('指令错误,请重新输入!!!')
if __name__ == '__main__':
start()
总结
来源:https://blog.csdn.net/hmh4640219/article/details/112910277
标签:Python,函数,万历表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python图像处理之图像量化处理详解
2023-06-09 15:07:30
![](https://img.aspxhome.com/file/2023/5/112535_0s.png)
通过实例简单了解Python sys.argv[]使用方法
2022-09-12 14:14:35
![](https://img.aspxhome.com/file/2023/4/113004_0s.png)
15款最佳jQuery LightBox插件
2010-09-25 12:41:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/4224386051_227c9e13d9-33s.jpg)
Python库functools示例详解
2021-03-10 07:01:27
Python 实现劳拉游戏的实例代码(四连环、重力四子棋)
2022-09-16 00:07:30
![](https://img.aspxhome.com/file/2023/2/110412_0s.png)
如何用用Python制作NFT区块链作品
2022-11-21 22:51:09
![](https://img.aspxhome.com/file/2023/4/82644_0s.jpg)
使用ajax开发的五大误区
2008-09-03 12:46:00
C#调用Python程序传参数获得返回值
2023-10-11 06:49:52
SQL 字母数字混合型字段 按里面的数字排序
2010-04-23 18:18:00
![](https://img.aspxhome.com/file/UploadPic/20104/23/1-47s.jpg)
使用SQL Server 2008中对象相关性
2008-12-12 06:36:00
Python tkinter之ComboBox(下拉框)的使用简介
2021-11-10 02:40:29
![](https://img.aspxhome.com/file/2023/3/127813_0s.png)
Python使用scrapy采集数据过程中放回下载过大页面的方法
2021-10-22 12:39:22
Django Form and ModelForm的区别与使用
2022-04-12 15:28:10
详解python中的json的基本使用方法
2022-03-15 22:29:13
python开发之list操作实例分析
2023-04-17 16:07:33
APIStar:一个专为Python3设计的API框架
2022-05-29 03:52:02
![](https://img.aspxhome.com/file/2023/1/114661_0s.png)
MySQL中SQL的单字节注入与宽字节注入
2009-03-25 14:49:00
Python如何使用队列方式实现多线程爬虫
2022-03-24 08:56:51
Python编程super应用场景及示例解析
2023-06-10 03:00:46
![](https://img.aspxhome.com/file/2023/7/117907_0s.png)
利用Python 实现图片转字符画
2022-05-06 11:31:01
![](https://img.aspxhome.com/file/2023/0/92510_0s.png)