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='\t\t')
    for i in range(1, func(year, month)+1):
        print(i, end='\t\t')
        if (i+week) % 7 == 0:
            print()

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


funa()

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

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值