python使用选择结构鉴别合法日期
2018-03-16 15:23
218 查看
用户将输入年月日三个整数,为了简化问题,我们假定程序只认定公元1年及之后的年份合法在这里需要特别注意的就是关于
闰年二月和平年2月的问题如果一个年份能被4整除但不能被100整除,或者能被400整除,则这一年为闰年。python代码清单
如下
#接收用户输入的年月日year=int(input("please input the year:"))month=int(input("please input the month:"))day=int(input("please input the day:"))#判断是否合法年份if year>0: if month in{1,3,5,7,8,10,12}: if 1<=day<=31: print("Valid date.") else: print("Invalid day.") elif month in{4,6,9,11}: if 1<=day<=30: print("Valid date.") else: print("Invaild day.") elif month==2: if(year%4==0 and year%100!=0)or(year%400==0): if 1<=day<=29: print("Vaild date.") else: print("Invaild day.") else: if 1<=day<=28: print("vaild date.") else: print("Invaild day.") else: print("Invaild month") else: print("Invaild year")
闰年二月和平年2月的问题如果一个年份能被4整除但不能被100整除,或者能被400整除,则这一年为闰年。python代码清单
如下
#接收用户输入的年月日year=int(input("please input the year:"))month=int(input("please input the month:"))day=int(input("please input the day:"))#判断是否合法年份if year>0: if month in{1,3,5,7,8,10,12}: if 1<=day<=31: print("Valid date.") else: print("Invalid day.") elif month in{4,6,9,11}: if 1<=day<=30: print("Valid date.") else: print("Invaild day.") elif month==2: if(year%4==0 and year%100!=0)or(year%400==0): if 1<=day<=29: print("Vaild date.") else: print("Invaild day.") else: if 1<=day<=28: print("vaild date.") else: print("Invaild day.") else: print("Invaild month") else: print("Invaild year")
相关文章推荐
- 类与结构的比较及它们的使用选择
- 如何使用Dojo的DatePicker控件制作联动日期选择器
- C language 使用if…else if…else语句实现多分支选择结构*(语法结构同C#)
- 使用枚举和结构输出日期
- My97DatePicker 日期选择器,使用的时候,如果IE6下打开使用该控件的html会出错 “无法打开站点,已终止操作”
- 使用if…else,if…else实现多分支选择结构
- 使用ole中的Microsoft Date and time picker control来添加日期选择框
- Python日期/时间操作方法使用
- Python中条件选择和循环语句使用方法介绍
- 文件系统层次结构分析、目录管理、文件管理、日期管理常用命令使用
- Delphi for iOS开发指南(5):在iOS应用程序中使用Calendar组件来选择日期
- 使用My97DatePicker实现日期选择并连接数据库
- QCalendar的使用:实现弹出日历选择日期的例子
- My97DatePicker 日期选择器,使用的时候,如果IE6下打开使用该控件的html会出错 “无法打开站点,已终止操作”
- 构建简单的 C++ 服务组件,第 2 部分: 通过服务组件体系结构使用 Python、Ruby 和 Web 服务
- Python中条件选择和循环语句使用方法介绍
- Calendar日期选择控件的使用
- 一百零一、在iphone应用程序当中 使用 日期选择器(UIDatePicker)
- ASP.NET基础教程-Calendar日期选择控件的使用
- 如何使用Dojo的DatePicker控件制作联动日期选择器