输入某年某月某日,判断这一天是这一年的第几天?
来源:互联网 发布:cad绘图软件中文版 编辑:程序博客网 时间:2024/06/05 15:22
__author__ = 'zhouhai'
#输入某年某月某日,判断这一天是这一年的第几天?
#判断是否是闰年
def is_leap_year(year):
if year%100 != 0 and year%4 == 0:
return True
if year%400 == 0:
return True
return False
#判断输入的日期是否合理?只有大月才有31天
def is_day_ok(year,month,day):
big_month = [1,3,5,7,8,10,12]
small_month = [4,6,9,11]
february = [2]
if day == 31 and month in big_month :
return True
elif day ==30 and month in small_month :
return True
elif day == 29 :
if month in february and is_leap_year(year)==False:
return False
else:
return True
elif day < 29 :
return True
else:
print("#########")
return False
if __name__ =='__main__':
date = input("请输入日期,格式如下:20170911")
year = date[0:4]
month = date[4:6]
day = date[6:8]
while date.isdigit() != True or len(date) != 8 or int(month)==0 or int(month)>12 or int(day)==0 or int(day)> 31 or is_day_ok(int(year),int(month),int(day))==False:
date = input("输入日期有误,格式如下:20170911")
year = date[0:4]
month = date[4:6]
day = date[6:8]
print("输入的日期是%s"%(date))
big_month = [1,3,5,7,8,10,12]
small_month = [4,6,9,11]
month_int = int(month)
days = 0
#开始算天数
for i in range(1,month_int):
if i in big_month:
days = days + 31
elif i in small_month:
days = days + 30
elif is_leap_year(int(year)):
days = days + 29
else:
days = days + 28
days = days + int(day)
#输入某年某月某日,判断这一天是这一年的第几天?
#判断是否是闰年
def is_leap_year(year):
if year%100 != 0 and year%4 == 0:
return True
if year%400 == 0:
return True
return False
#判断输入的日期是否合理?只有大月才有31天
def is_day_ok(year,month,day):
big_month = [1,3,5,7,8,10,12]
small_month = [4,6,9,11]
february = [2]
if day == 31 and month in big_month :
return True
elif day ==30 and month in small_month :
return True
elif day == 29 :
if month in february and is_leap_year(year)==False:
return False
else:
return True
elif day < 29 :
return True
else:
print("#########")
return False
if __name__ =='__main__':
date = input("请输入日期,格式如下:20170911")
year = date[0:4]
month = date[4:6]
day = date[6:8]
while date.isdigit() != True or len(date) != 8 or int(month)==0 or int(month)>12 or int(day)==0 or int(day)> 31 or is_day_ok(int(year),int(month),int(day))==False:
date = input("输入日期有误,格式如下:20170911")
year = date[0:4]
month = date[4:6]
day = date[6:8]
print("输入的日期是%s"%(date))
big_month = [1,3,5,7,8,10,12]
small_month = [4,6,9,11]
month_int = int(month)
days = 0
#开始算天数
for i in range(1,month_int):
if i in big_month:
days = days + 31
elif i in small_month:
days = days + 30
elif is_leap_year(int(year)):
days = days + 29
else:
days = days + 28
days = days + int(day)
print("这个是一年中的第%d天"%(days))
总结:
这个程序并不难;
麻烦的在于不仅要计算天数,还要先判断用户的输入是否合法;比如输入的2月30日;或者输入的是2月29日,但是又不是闰年;或者输入4月31日,4月是没有31日的。
阅读全文
0 0
- 输入某年某月某日,是闰年还是平年,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天?考虑闰年的情况
- 输入某年某月某日,判断这一天是一年的第几天 的三种方式
- C语言经典算法:输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天
- 题目:输入某年某月某日,判断这一天是这一年的第几天?
- 【程序3】题目:输入某年某月某日,判断这一天是这一年的第几天?
- [Java] 练习题014: 输入某年某月某日,判断这一天是这一年的第几天?
- 2.输入某年某月某日,判断这一天是这一年的第几天?
- c语言:/*题目:输入某年某月某日,判断这一天是这一年的第几天?*/
- 输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天?
- 【程序14】 题目:输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天
- 题目:输入某年某月某日,判断这一天是这一年的第几天?
- 输入某年某月某日,判断这一天是这一年的第几天?
- ★输入某年某月某日,判断这一天是这一年的第几天? C/C++
- java经典算法_013输入某年某月某日,判断这一天是这一年的第几天?
- Python教程之七-----编写你的第一个Django应用(6)
- B树、B-树、B+树、B*树
- JAVA中函数
- oracle的分页查询
- Oracle数据库基础
- 输入某年某月某日,判断这一天是这一年的第几天?
- Run-Time Check Failure #2
- 采用mustache语言作为elasticsearch搜索请求的预处理模板
- 在eclipse如何设置新建JSP文件的默认字符编码为UTF-8?
- Unity3d--GUI自适应矩阵(通过Matrix4x4.SetTRS)
- hadoop学习3
- [转载]图论500题
- 计算机网络第一节
- unity 如何实现安卓Android的toast功能