|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以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[month - 1]
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)) |
|