【Python】Python_learning5:使用python计算生日日期
2016-08-29 16:11
295 查看
Python_Learning5:
题目:输入某年某月某日,判断这一天是这一年的第几天?程序分析:
1> 年:平年2月28;闰年2月29;
2> 月:注意月在闰年还是平年,并且每个月的天数不一样;
1-3-5-7-8-10-12为31天;
4-6-9-11都是30日;
Example:以3月13日为例,应该先把前两个月的加起来,然后再加上13天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天:
------------------------------------------------------------------------------------------------------------------------------------
Code ResourceListing:
#!/usr/bin/python # -*- coding: UTF-8 -*- %<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,</span><span style="font-family: Arial; font-size: 14px;">否则python2会默认使用ASCII编码。</span>
year = int(input('year年:\n')) #定义整型变量year month = int(input('month月:\n')) #定义整型变量month day = int(input('day日\n')) #定义整型变量day
months = (0,31,59,90,120,151,181,212,243,273,304,334) #月的天数, 按月递增,months=前面月份天数之和 if 0 < month < 12: #从1月份到11月份 #if 0 <= month <= 12: sum = months[month - 1] else: print('error data') sum += day #前面所有的月份天数之和 加上 当月天数
leap = 0 #这个值为平年和闰年做准备 if(year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): leap = 1 if(leap == 1)and (month > 2): sum + = 1
print('it is the %dth day.' %sum)
-----------------------------------------------------------------------------------------------------------------------------------
相关文章推荐
- 根据一个人的出生日期和今天的日期,计算他最近的生日(使用Northwind数据库的Employees表)
- 计算Java日期--学习怎样创建和使用日期(1)
- 计算Java日期:学习怎样创建和使用日期
- python中使用time模块计算代码执行效率的精度测试
- Python日期/时间操作方法使用
- python写的计算两个日期之间的天数的脚本
- 计算Java日期--学习怎样创建和使用日期
- Python:通过计算阶乘来学习lambda和reduce这两个函数的使用
- 使用Javascript计算时间差和计算日期加天数后的日期值
- windows7下使用64位Python编程、科学计算、绘制图表
- 计算Java日期--学习怎样创建和使用日期
- 用python计算top命令中某个进程的CPU,Memory使用情况
- [转]使用python调用计算所分词
- Python实例讲解 -- 获取本地时间日期(日期计算)
- python 计算下一个星期三的日期
- 根据生日的月份和日期,一行代码计算星座的js小函数(最简)
- 使用Powershell计算自己活了多少天,以及反推自己或别人的生日
- sqlserver 日期计算(日期函数的熟练使用)
- Python:通过计算阶乘来学习lambda和reduce这两个函数的使用
- 使用python 在已知圆弧两个端点和中点坐标 计算圆弧的圆心坐标