python
来源:互联网 发布:软件项目验收流程 编辑:程序博客网 时间:2024/06/04 01:30
写一个函数,计算给定日期是该年的第几天.
满足闰年的条件:
闰年是公历中的名词,能被4整除但不能被100整除,或能被400整除的年份即为闰年。 闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。 凡阳历中有闰日(二月为二十九日)的年;闰余(岁余置闰。阴历每年与回归年相比所差的时日);注意闰年(公历中名词)和闰月(农历中名词)并没有直接的关联,公历中只分闰年和平年,平年有365天,而闰年有366天(2月中多一天);平年中也可能有闰月(如2017年是平年,农历有闰月,闰6月)。
代码如下:
#coding=utf-8#写一个函数,计算给定日期是该年的第几天.def count(year,month,day): count = 0 #判断该年是平年还是闰年 if year%400==0 or (year%4==0 and year%100!=0): print('%d年是闰年,2月份有29天!'%year) li1 = [31,29,31,30,31,30,31,31,30,31,30,31] for i in range(month-1): count += li1[i] return count+day else: print('%d年是平年,2月份有29天!' % year) li2 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for i in range(month-1): count +=li2[i] return count+dayif __name__ == "__main__": year = int(input('请输入年份:')) month = int(input('请输入月份:')) day = int(input('请输入日期:')) count = count(year,month,day) print('%d年%d月%d日是今年的第%d天!'%(year,month,day,count))
运行结果:
请输入年份:2017请输入月份:8请输入日期:172017年8月17日是今年的第229天!
阅读全文
1 0
- Python
- Python
- Python
- python
- Python
- PYTHON
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- python
- Python
- 51nod 1099 任务执行顺序 ACM
- 在Debian上安装完整的内核源码
- OpenCV编译遇到的一个常见错误-./bin: error while loading shared libraries: libopencv_highgui.so.3.2: cannot open
- margin负值问题
- 计算机视觉课程
- python
- 动态代理模式
- THINKPHP查询
- 正则表达式(3)之常用函数
- [笔记]对拍
- Kattis
- SICK--LMS11x雷达在ROS中的使用
- HDU 6140 Hybrid Crystals(思考+上下界)
- 修建泳池