题目:

输入某年某月某日,判断这一天是这一年的第几天?

程序分析:

以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的更多相关文章

  1. Python每日一题 004

    将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...

  2. Python每日一题 003

    将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...

  3. Python每日一题 002

    做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...

  4. Python每日一题 009

    题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...

  5. Python每日一题 008

    题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...

  6. Python每日一题 007

    题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...

  7. Python每日一题 006

    题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...

  8. Python每日一题 005

    任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...

  9. Python每日一题 001

    Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...

随机推荐

  1. 使用Tensorflow操作MNIST数据

    MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会被用作深度学习的入门样例.而TensorFlow的封装让使用MNIST数据集变得更加方便.MNIST数据集是NIST数据集的 ...

  2. Scrapy实战篇(四)爬取京东商城文胸信息

    创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...

  3. [UE4]Safe Zone:安全区域

    一.在做移动开发的时候,为了避免被手机上的硬件元素挡住界面,就可以使用Safe Zone控件,如下图所示的棕色区域就是Apple IphoneX的课被挡住界面的区域:上面的是Iphone的喇叭位置,下 ...

  4. .net 连接 Oracle 可能需要配置

    D:\Program Files (x86)\Oracle Developer Tools for VS2013\network\admin\tnsnames.ora

  5. MySQL数据库的库表基本操作

    一.库操作 1.创建业务数据库 DDL 数据库命名规则:区分大小写.唯一性.不能使用关键字如 create select.不能单独使用数字 语法:CREATE DATABASE 数据库名; CREAT ...

  6. JS基础——变量

    引用类型:对象  数组 函数 }; var b =a ; b.age = ; console.log(a.age);// 21 传递的是地址, a,b同地址 值类型: var a =100; var ...

  7. 换上 SansForgetica-Regular 字体,增加记忆能力

    最近澳大利亚的RMIT(皇家墨尔本理工大学) 搞出来这么个字体,号称能增强记忆,原理是通过难以识别的字体,让人提起精神去识别,从而记忆更深刻. 果断弄了个试试. 安装过程: 下载字体文件 点这里去下载 ...

  8. linux文件压缩解压命令

    01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...

  9. 用python探索和分析网络数据

    Edited by Markdown Refered from: John Ladd, Jessica Otis, Christopher N. Warren, and Scott Weingart, ...

  10. !!代码:baidu地图

    http://map.baidu.com/mobile/  手机开发时,嵌入地图可以嵌入这个网址!! http://developer.baidu.com/map/lbs-cloud.htm 百度地图 ...