栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现一个万年历

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现一个万年历

# b 判断闰年
def funb(year):
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        return True
    return False

# c 判断某月总天数
def func(year, month):
    days = 31
    if month == 2:
        if funb(year):
            days = 29
        else:
            days = 28
    elif month in (4, 6, 9, 11):
        days = 30
    return days

# d 某年距离1900年的总天数
def fund(year):
    res = 0
    for i in range(1900, year):
        if funb(i):
            res += 366
        else:
            res += 365
    return res

# e 某月距离当年1月1日的总天数
def fune(year, month):
    res = 0
    for i in range(1, month):
        res += func(year, i)
    return res

# f 根据d和e求总天数
def funf(year, month):
    return fund(year) + fune(year, month)

# g 求某天是星期几
def fung(year, month):
    res = funf(year, month)
    # 当前天
    week = (res+1) % 7
    return week

# h 打印日历
def funh(year, month):
    week = fung(year, month)
    # print('week', week)
    print('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', sep='t')
    # 星期week 空格
    for i in range(week):
        print(' ', end='tt')
    for i in range(1, func(year, month)+1):
        print(i, end='tt')
        if (i+week) % 7 == 0:
            print()

# a 获得年份和月份
def funa():
    year = int(input('请输入年份:'))
    month = int(input('请输入月份:'))
    funh(year, month)


funa()

以1900年1月1日(星期一)为基准点 实现用户输入年份和月份打印当月的日历功能

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号