Python:每日一题004
题目:
输入某年某月某日,判断这一天是这一年的第几天?
程序分析:
以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
个人的思路及代码:
- month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 ]
- while True:
- year = input("请输入年份:").strip()
- month = input("请输入月:").strip()
- day = input("请输入天:").strip()
- if not year.isdigit() or not month.isdigit() or not day.isdigit():
- print("您的输入有误请重新输入!")
- continue
- else:
- year = int(year)
- month = int(month)
- day = int(day)
- if month > 12 or day > 31 or day < 0:
- print("您的输入有误请重新输入!")
- continue
-
- if (year % 4 == 0 and year %100 != 0) or year % 400 == 0:
- if month > 2:
- index_day = sum(month_days[:month-1]) + day + 1
- else:
- index_day = sum(month_days[:month-1]) + day
- else:
- index_day = sum(month_days[:month-1]) + day
- print("这一天是一年中的第%s天" % index_day)
分析:这里考虑了大部分输入异常的情况,但是还是有输入错误但是不能检测出来的情况,比如输入4月31日不能检测出日期不正确。
再次修改增加判断条件,检测大小月和2月的问题
- month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 ]
- while True:
- year = input("请输入年份:").strip()
- month = input("请输入月:").strip()
- day = input("请输入天:").strip()
- if not year.isdigit() or not month.isdigit() or not day.isdigit():
- print("您的输入有误请重新输入!")
- continue
- else:
- year = int(year)
- month = int(month)
- day = int(day)
- if month > 12 or day > 31 or day < 0:
- print("您的输入有误请重新输入!")
- continue
- elif month in [4,6,9,11] and day > 30 or month == 2 and day > 29:
- print("您的输入有误请重新输入!")
- continue
- if (year % 4 == 0 and year %100 != 0) or year % 400 == 0:
- if month > 2:
- index_day = sum(month_days[:month-1]) + day + 1
- else:
- index_day = sum(month_days[:month-1]) + day
- else:
- if month == 2 and day > 28:
- print("您的输入有误请重新输入!")
- continue
- index_day = sum(month_days[:month-1]) + day
- print("这一天是一年中的第%s天" % index_day)
其他参考解答:
参考1
- def leapyear(n):
- return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False
-
- days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ]
- year, month, day = [int(x) for x in input('input year/month/day: ').split('/')]
- #直接用列表解析式获取三个数据
- day2 = sum(days[:month - 1]) + day
- if leapyear(year) and month > 2:
- day2 += 1
- print(day2)
参考2
- import datetime
- x=int(input("请输入年份xxxx:"))
- y=int(input("请输入月份xx:"))
- z=int(input("请输入日xx:"))
- n1=datetime.date(x,y,z)
- n2=datetime.date(x,1,1)
- n3=n1-n2
- n3=int(n3.days)+1
- print("这是这一年的第%s天"%n3)
分析:这里用datetime模块避免了输入日期不正确的情况,如果输入不正确则直接报错。
(本文编号004,首发于2018年9月14日,修改于2018年9月15日)
Python:每日一题004的更多相关文章
- Python每日一题 004
将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...
- Python每日一题 003
将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...
- Python每日一题 002
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...
- Python每日一题 009
题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...
- Python每日一题 008
题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...
- Python每日一题 007
题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...
- Python每日一题 006
题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...
- Python每日一题 005
任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...
- Python每日一题 001
Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...
随机推荐
- 为嵌入式mplayer移植添加ALSA音频驱动(全志V3s荔枝派zero)
首先准备mplayer和alsa_lib,我的是bulidroot添加后编译自动下载的,版本分别是alsa-lib-1.1.4.1和mplayer-1.3.0. 首先编译alsa_lib: ./con ...
- Android SurfaceView及TextureView对比
SurfaceView是什么? 它继承自类View,因此它本质上是一个View.但与普通View不同的是,它有自己的Surface.有自己的Surface,在WMS中有对应的WindowState,在 ...
- Windows不要使用记事本编辑文本文件
摘自:廖雪峰 千万不要使用Windows自带的记事本编辑任何文本文件.原因是Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0x ...
- 12月中旬项目中出现的几个bug解决方法的思考
这周做的项目遇到2个费了很多时间才解决的bug,解决之后,发现根本问题并不是什么很难的技术难点,都是因为自己在写代码的过程中,思维不够清晰.还有一个需要再提高的地方就是解决问题的思维,如何快速定位到问 ...
- JVM优化系列之一(-Xss调整Stack Space的大小)
Java程序中,每个线程都有自己的Stack Space(堆栈).这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅 ...
- servlet cdi analysis
CDI中最令人兴奋的功能是允许每个人在Java EE平台中编写强大的扩展性功能,甚至于改变其核心本身.这些扩展性功能是可以完全移植到任何支持CDI的环境中. CDI的一些主要特性 1.类型安全:CDI ...
- mybatis的简单使用调用mapper接口
mybatis 是apache下的一个面向sql编程的半自动化的ORM持久层的框架.特点:面向sql编程,达到高性能的使用目的. 下面是简单使用 现导入jar包,只有mybatis和数据库驱动包(这里 ...
- 模拟实现简单ATM功能
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
1.引用Microsoft.Office.Interop.Excel.dll 2.引用命名空间.使用别名 using System.Reflection; using Excel = Microsof ...
- leetcode33
class Solution { public: int search(vector<int>& nums, int target) { //这个题是给一个排序数组,但是数组里面内 ...