Python中列表实现:输入年月日,输出本年的第?天

题目描述:
1、输入年月日,计算出它是本年的第几天
年月日格式:YYYY-MM-DD

2、列表实现
代码块:

  time = input('请输入日期 YYYY-MM-DD:')
  date = time.split('-')
  year = int(date[0])
  month = int(date[1])
  day = int(date[2])

  li = [31,28,31,30,31,30,31,31,30,31,30,31]
  num = 0

  if ((year % 4 ==0) and (year % 100 != 0) or (year % 400 == 0)):
      li[1] = 29
  for i in range(12):
      if month > i + 1:
          num += li[i]
      else:
          num += day
          break
  print('这一天是%d年的第%d天' %(year,num))

示例及运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42566251/article/details/94725527