Python实现输入某年某月某日,判断这一天是这一年的第几天

2021-08-14 263阅读

文章最后更新时间:2022年11月02日

year = int(input("请输入某年"))
month = int(input("请输入某月"))
day = int(input("请输入某日"))
s = 0
daylist = [0,31,60,91,121,152,182,213,244,274,305,335,366]
if month == 1:
    if day < 32:
        s = day
    else:
        s = "NaN"
        print("请输入正确的天数")
else:
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        if 1 < month < 3:
            if day < 30:
                s = int(daylist[month - 1]) + day
            else:
                s = "NaN"
                print("请输入正确的天数")
        elif 1 < month < 13:
            s = int(daylist[month - 1]) + day
        else:
            s = "NaN"
            print("请输入正确的月份")
    else:
        if 1 < month < 3:
            if day < 29:
                s = int(daylist[month-1])+day
            else:
                s = "NaN"
                print("请输入正确的天数")
        elif 2 < month < 13:
            s = int(daylist[month - 1]) + day -1
        else:
            s = "NaN"
            print("请输入正确的月份")
print("%d年%d月%d日 是%d年的第%d天" % (year, month, day, year, s))
文章版权声明:除非注明,否则均为XLX笔记原创文章,转载或复制请以超链接形式并注明出处。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码