您的位置:首页 > 编程语言 > Python开发

【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)


-----------------------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: