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)
代码编辑器
测试用例输入
{{resultStatus.text}}