输入年月日算出这是今年的第几天:
题目:输入某年某月某日,判断这一天是这一年的第几天?程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
思路:
先算出每个月有多天保存在months列表中,输入的月份减1,加上这个输入的天数,如果输入的月份不在1-12,则提示输入的月份不对,再判断输入的年份是否是闰年,如果是闰年再在原来的天数上加1
代码:
year = int(input('year:'))
month = int(input('month:'))
day = int(input('day:'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month and month <= 12:
sum = months
else:
print('亲,一年只有12个月!')
sum += day
l = 0
if year % 4 == 0 or year % 400 == 0:
l = 1
if l == 1 and month > 2:
sum += 1
print('这是 %s 年的第 %s 天!' % (year,sum)) 写的不错,继续加油
页:
[1]