温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python输出一个万历表

发布时间:2021-01-25 16:13:51 来源:亿速云 阅读:158 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用Python输出一个万历表,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

示例代码

# 判断是否闰年
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()

上述就是小编为大家分享的使用Python输出一个万历表了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI