第15457题 程序题
Python编写并完善getDays函数计算指定日期是当年的第几天

题目要求

输入格式为YYYYMMDD的字符串日期,计算并返回这一天是这一年的第几天,需完善给定的Python函数代码。

函数规范

  • 函数名:getDays
  • 参数:year,字符串类型,格式如"20211201"
  • 返回值:整数,表示第几天

待完善代码

def getDays(year):
    s = 0
    y = int(year[:4])
    m = int(year[4:6])
    d = int(year[6:8])
    monthdays = (31,28,31,30,31,30,31,31,30,31,30,31)
    for i in range(  ①  ):
        s = s +    ②      
    if (y % 4 ==0 and y % 100 !=0 or y % 400 ==0) and   ③  :
        s = s + 1
    s = s + d
    return s

year = "20211201"
theday =   ④  
print(theday)
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
编辑模式
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析