编程04
输入某年某月某日,判断这一天是这一年的第几天?
原理:闰年比平年多一天,区别是平年2月是28天,
闰年是29天,所以在月份大于2时候,闰年比平年多一天,
故需要判断年份是闰年和平年否?然后在做出判断。
代码:
year = int(input('year:\n'))
month = int(input('month:\n'))
day = int(input('day:\n'))
months = (0,31,59,90,120,151,181,212,243,273,304,334,365)
if 0<month<=12:
sum = months[month-1]
else:
print('error month\n')
sum = sum+day
flog = 0#标记为平年
if (year % 400 ==0)or ((year % 100==0)and (year % 4==0)):#判断为闰年,标记为flog=1
flog =1
else:
flog = 0
if((flog)and month>=3):#flog为真也就是flog==1,且月份大于等于3,也就是大于2时候,总天数:闰年比平年多一天
sum = sum+1
print('It is the %dth day.' %sum)
运行结果:
year:
2019
month:
12
day:
22
It is the 356th day.
总结:
- 1、输入具体的年月日;
- 2、按次序将月数的每个天数累加,分别用python字典表示出来;
- 3、将在合适的月份范围类,计算出输入月份的天数,其次在加上键盘输入的天数;
- 4、最初是定义平年故标记flog为0,后面需判断年是否为闰年根据**if (year % 400 =0)or ((year % 100=0)and (year % 4=0))**成立的话,flog标记为1;
- 5、最后判断flog为1的时候且月数大于2的时候,它的总天数要加1.