如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# -*- coding: utf-8 -*- # 简述:要求输入某年某月某日 # 提问:求判断输入日期是当年中的第几天? def which_day(year,month,day): list = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] whichday = 0 if (year % 4 ) = = 0 and (year % 100 )! = 0 or (year % 400 ) = = 0 : list [ 1 ] = 29 for i in range ( 1 ,month): if month = = 1 : print day whichday = whichday + list [i - 1 ] whichday = whichday + day print whichday if __name__ = = "__main__" : year = int ( raw_input ( "请输入年份:" )) month = int ( raw_input ( "请输入月份:" )) day = int ( raw_input ( "请输入天:" )) which_day(year, month, day) |
运行结果:
请输入年份:2017
请输入月份:3
请输入天:31
90
总结:
1、闰年的判断为能被4整除,但不能被100整除,或者能被400整除
刚开始三者都写成了and
2、关于list[i-1]刚开始写成了i-2,对于某些时期恰好是正确的,是因为成了list[-1]时从结尾开始取值
以上这篇python判断输入日期为第几天的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Guo_Apple/article/details/68922832