python给出年月日,确定它是该年的第几天

任务3、给出年月日,确定它是该年的第几天

小知识:要判断是否为闰年

# -*- coding: utf-8 -*-
"""
功能:判断是该年第几天
作者:cxf
日期:2021年11月20日
"""
# 输入部分
while 1:
    year = int(input('年:\n'))     
    month = int(input('月:\n'))
    day = int(input('日:\n'))
 # 判断部分
    months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)   #把每月的前几个月的天数加起来
    if 0 < month <= 12 and day <= 31: #判断是否输入错误
        sum = months[month - 1]
        sum += day
        n = 0
        if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):  #判断是否为闰年
            n = 1
        if (n == 1) and (month > 2):
            sum += 1
        print('这是{}年的第{}天' .format (year, sum))
        break
    else:
        print('输入错误,请重新输入!\n')

Guess you like

Origin blog.csdn.net/qq_62590351/article/details/121446458