【Python3】日期判断

2 阅读1分钟

题目 输入某年某月某日,判断这一天是这一年的第几天?

def isLeapYear(y):
    # 如果年份能被400整除,或者能被4整除但不能被100整除,则返回True,否则返回False
    return (y%400==0 or (y%4==0 and y%100!=0))
# 定义一个列表,存储每个月的天数
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
# 初始化一个变量,用于存储结果
res=0
# 输入年份、月份和日期
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
# 如果是闰年,则将2月的天数加1
if isLeapYear(year):
    DofM[2]+=1
# 遍历月份,累加每个月的天数
for i in range(month):
    res+=DofM[i]
# 输出结果
print(res+day)