您的位置:首页 > 其它

程序4--日期是一年的第几天

2017-05-09 17:27 375 查看
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:

def number_of_days():
year = int(input("year:"))
month = int(input("month:"))
day = int(input("day:"))
months = [0,31,59,90,120,151,182,212,243,273,304,334]
num = 0
if 0<month<=12:
num = months[month-1]
else:
print("Data error")
num += day
if (year % 400 == 0)or((year % 4 == 0)and(year % 100 != 0)):
if month > 2:
num +=1
print('it is the %dth day.' % num)



运行:
>>> number_of_days()
year:2024
month:5
day:9
it is the 130th day.

另外一些方法:
import time
def number_of_days2():
a = input("输入时间(格式如:2017-04-04):")
t = time.strptime(a,"%Y-%m-%d")
print(time.strftime("今年的第%j天",t))


另外:
>>> p = [31,28,31,30,31,30,31,31,30,31,30,31]
>>> sum(p[0:2])
59

可以求数组的p的某一段的和。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐