python 实例四
https://www.cnblogs.com/evablogs/p/6754981.html
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:
月份天数:
| 月份 | 天数 |
| 2 | 平年28天,闰年29天 |
| 1,3,5,7,8,10,12 | 31 |
| 4,6,9,11 | 30 |
闰年:
2、整百年:能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> L=[31,31,30,31,30,31,31,30,31,30,31]>>> def caldate(a,b,c): s=0 if(a%100!=0 and a%4==0 or a%100==0 and a%400==0): L.insert(1,29) else: L.insert(1,28) for i in range(b-1): s=s+L[i] return s+c>>> caldate(2016,1,2)2>>> caldate(2016,3,2)62 |
改进版:考虑了月份和天数的有效性(哈哈,对比网上的答案,一看自己的代码就像是菜鸟级的,还有很多需要学习的地方)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def caldate(a,b,c): L=[31,31,30,31,30,31,31,30,31,30,31] s=0 Leap=0 if(a%100!=0 and a%4==0 or a%100==0 and a%400==0): L.insert(1,29) Leap=1 else: L.insert(1,28) if 0<b<=12: if 0<c<=31: if((b==2)and(Leap==1)and(c<=29)or((b==2)and(Leap==0)and(c<=28))): for i in range(b-1): s=s+L[i] return s+c else: print 'The February should not greater than 28 or 29' else: print 'The date is error' else: print 'The month is invalid' |
输出:
|
1
2
3
4
5
6
7
8
9
10
|
>>> caldate(2016,4,33)The date is error>>> caldate(2017,2,30)The February should not greater than 28 or 29>>> caldate(2017,2,28)59>>> caldate(2017,2,29)The February should not greater than 28 or 29>>> caldate(2017,13,29)The month is invalid |
网上答案:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/python# -*- coding: UTF-8 -*- year = int(raw_input('year:\n'))month = int(raw_input('month:\n'))day = int(raw_input('day:\n')) months = (0,31,59,90,120,151,181,212,243,273,304,334)if 0 < month <= 12: sum = months[month - 1]else: print 'data error'sum += dayleap = 0if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): leap = 1if (leap == 1) and (month > 2): sum += 1print 'it is the %dth day.' % sum |
输出结果:
|
1
2
3
4
5
6
7
|
year:2015month:6day:7it is the 158th day. |
python 实例四的更多相关文章
- python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (二)
Hey! 如果你还没有看这篇的上文的话,可以去稍稍瞅一眼,会帮助加速理解这一篇里面涉及到的递归结构哦!(上一篇点这里:<python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)> ...
- Python 基础 四 面向对象杂谈
Python 基础 四 面向对象杂谈 一.isinstance(obj,cls) 与issubcalss(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls ...
- 【Python实例一】使用minidom读取xml文件
前言:最近刚在廖雪峰老师的网站里学习了Python的基础内容,想着循序渐进地找点实例练练手,网上看到有很多相关资料,决定针对感兴趣的内容实际编码实践一下,昨天刚好看到有关使用Python来读取XML文 ...
- 初识Python(四)
一.数字数据类型 Python的数字数据类型用于存储数值,它是不可变的数据类型,这意味着改变数字数据类型,则需要一个新分配的对象: Python支持四种不同的数值类型: 整型(Int):通常被称为是整 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 第一个python实例--监控cpu
#第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...
- (转)Python实例手册
原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...
- 使用docker安装部署Spark集群来训练CNN(含Python实例)
使用docker安装部署Spark集群来训练CNN(含Python实例) http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器 ...
- C语言库函数大全及应用实例四
原文:C语言库函数大全及应用实例四 [编程资料]C语言库函数大全及应用实例四 couble fmod (double x, dou ...
随机推荐
- [Swift]LeetCode948. 令牌放置 | Bag of Tokens
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be us ...
- 5.Git基础-撤销操作、标签的使用、Git别名
1.撤销操作 1.1 修改上一次的提交(commit)-- git commit --amend 有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git ...
- 【转】ret,retf,iret的区别
ret RET, and its exact synonym RETN, pop IP or EIP from the stack and transfer control to the new ad ...
- DOM事件第二弹(UIEvent事件)
此文章主要总结UIEvent相关的事件,如有不对的地方,欢迎指正. 一.uitls.js(绑定事件公共类) var fixs = { 'focusin': { standard: 'focus', i ...
- 并发编程(十一)—— Java 线程池 实现原理与源码深度解析(一)
史上最清晰的线程池源码分析 鼎鼎大名的线程池.不需要多说!!!!! 这篇博客深入分析 Java 中线程池的实现. 总览 下图是 java 线程池几个相关类的继承结构: 先简单说说这个继承结构,E ...
- redis 系列10 字符串对象
一. 字符串对象编码 Redis中字符串可以存储3种类型,分别是字节串(byte string).整数.浮点数.在上章节中讲到字符串对象的编码可以是int, raw,embstr. 如果一个字符串对象 ...
- Unable to build: the file dx.jar was not loaded from the SDK folder
eclipse 运行 android 时失败了,提示 Unable to build: the file dx.jar was not loaded from the SDK folder! 解决办法 ...
- ELK-log4j2异步输出+logstash
1.pom.xml配置文件 <dependency> <groupId>log4j</groupId> <artifactId>log4j</ar ...
- Linux命令收集
文件处理命令:ls 功能描述:显示目录文件 命令英文原意:list 命令所在路径:/bin/ls 执行权限:所有用户 语法: ls 选项[-ald] [文件或目录] -a 显示所有文件,包 ...
- 【c#】RabbitMQ学习文档(七)C# API
今天这篇博文是我翻译的RabbitMQ的最后一篇文章了,介绍一下RabbitMQ的C#开发的接口.好了,言归正传吧. Net/C# 客户端 API简介 主要的命名空间,接口和类 定义核心的API的接口 ...