|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求输入年月日后,计算输入的日期是当年的第几天,我的代码如下:
- date = input('请输入年月日(格式为xxxx-xx-xx,分别表示年月日:')
- month = [1,2,3,4,5,6,7,8,9,10,11,12]
- day1 = [31,28,31,30,31,30,31,31,30,31,30,31] #闰年的月份天数
- day2 = day1.copy()
- day2[1] = 29 #闰年的月份天数
- days = 0
- #先求闰年的天数计算,此时要用到day2里的数据
- if int(date[:4]) % 100 ==0:
-
- for i in range(0,12):
- if int(str(date[5:7])) < month[i]: #用int(str(date[5:7]))目的是将输入的月份中,前置的0去掉
- days += day2[i] #先让前面月份的天数进行循环相加,
-
- days = days + int(str(date[8:10])) #跳出循环后,再加上输入月份的天数
- #第二种闰年情况的天数计算
- elif int(date[:4]) % 4 ==0 and int(date[:4]) % 100 !=0:
- for i in range(0,12):
- if int(str(date[5:7])) < month[i]:
- days += day2[i]
-
- days = days + int(str(date[8:10]))
- #非闰年情况的天数计算
- else:
- if int(str(date[5:7])) < month[i]:
- days += day1[i] #非闰年部分,需要用到非闰年的月份数据
-
- days = days +int(str(date[8:10]))
复制代码
不知道为什么运行不了,报错说month[i]中的i没有定义? 请高手指点下。。
|
|