python2.7练习小例子(四)
4):题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天。
程序源代码:
#!/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 += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum
以上实例输出结果为:
year:
2015
month:
6
day:
7
it is the 158th day.
看另外一个案例:
#!/usr/bin/python
# -*- coding: UTF-8 -*- year=int(raw_input("年:\n"))
month=int(raw_input("月:\n"))
day=int(raw_input("日:\n"))
months1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年
months2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年 if ((year%4==0)and(year%100!=0)) or((year%100==0)and(year%400==0)):
Dth=months1[month-1]+day
else:
Dth=months2[month-1]+day
print "是该年的第%d天"%Dth
闰年需要同时满足以下条件:
- 1、年份能被4整除;
- 2、年份若是 100 的整数倍的话需被400整除,否则是平年。
#!/usr/bin/python
# -*- coding: UTF-8 -*- # 输入任意年月日,知道是改年第几天 p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年
w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年 year =int(raw_input("请输入年:"+'\n'))
month =int(raw_input("请输入月:"+'\n'))
day=int(raw_input("请输入日:"+'\n')) arr=[31,28,31,30,31,30,31,31,30,31,30,31]
sum=day
for i in range(0,month-1):
sum+=arr[i]
if year%4==0:
if year%100==0 and year%400!=0:
print "这是今年的第%d天" % sum
else:
sum=sum+1
print "这是今年的第%d天" % sum
else:
print "这是今年的第%d天" % sum
再来参考一个:
#!/usr/bin/python
# -*- coding: UTF-8 -*- # 输入任意年月日,知道是改年第几天 p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年
w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年 year =int(raw_input("年:\n"))
month =int(raw_input("月:\n"))
day =int(raw_input("日:\n")) # 判断闰年,平年 if year % 100 == 0:
if year % 400 == 0:
d=w
else:
d=p
else:
if year % 4 == 0:
d=w
else:
d=p # 计算天数 days = sum(d[0:month-1])+day
print "%d.%d.%d是%d年的第%s天。"%(year,month,day,year,days)
还有哦:
#!/usr/bin/python
# -*- coding: UTF-8 -*- year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
day = int(input('请输入日期:'))
total = 0
if year%4 == 0:
days = 29
else:
days = 28
if year%4 == 0:
print year, '年是润年,2月有', days, '天!'
else:
print year, '年是平年,2月有', days, '天!'
if month <= 1:
total = day
elif month == 2:
total = 31 + day
elif month == 9 or month == 11:
total = (month - 2) * 30 + days + month/2 + day + 1
else:
total = (month - 2) * 30 + days + month/2 + day
print year, '年',month, '月', day, '日,是这一年的第', total, '天!'
完事再来看一个:
#!/usr/bin/python def isLeapYear(a):
if (0 == a%4 or 0 == a%400) and 0 != a%100 :
return 1
else:
return 0
dict = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
a = int(input("Input year:"))
b = int(input("Input month:"))
c = int(input("Input day:"))
m = 0
k = isLeapYear(a)
for i in range(1,b):
m = m + dict[i]
m = m + isLeapYear(a) + c print(m)
#!/usr/bin/env python
# -*- coding: utf-8 -*- year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))
days = [31,28,31,30,31,30,31,31,30,31,30,31]
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
days[2] += 1 now = sum(days[0:month-1])+day
print 'it is the %dth day.' %now
#!/usr/bin/env python
#coding:utf-8 import time
import sys reload(sys)
sys.setdefaultencoding('utf-8') # 设置 'utf-8'
a = raw_input("输入时间(格式如:2017-04-04):")
t = time.strptime(a,"%Y-%m-%d")
print time.strftime("今年的第%j天",t).decode('utf-8')
#! /usr/bin/env python
# coding:utf-8 import time D=raw_input("请输入年份,格式如XXXX-XX-XX:")
d=time.strptime( D,'%Y-%m-%d').tm_yday
print "the {} day of this year!" .format(d)
Python3 参考解法:
#!/usr/bin/python3
date = input("输入年月日(yyyy-mm-dd):")
y,m,d = (int(i) for i in date.split('-'))
sum=0
special = (1,3,5,7,8,10)
for i in range(1,int(m)):
if i == 2:
if y%400==0 or (y%100!=0 and y%4==0):
sum+=29
else:
sum+=28
elif(i in special):
sum+=31
else:
sum+=30
sum+=d
print("这一天是一年中的第%d天"%sum)
再来看:
#!/usr/bin/python
# -*- coding: UTF-8 -*- i= int(input('请输入年份:'))
j= int(input('请输入月份:'))
k= int(input('请输入天数:'))
month = [31,28,31,30,31,30,31,31,30,31,30,31]
day = 0
if i%4 == 0 or i %400 == 0 and i%100 != 0: #闰年
month = month[:j-1]
if j >2: #大于2月份
day = sum(month)+1+k elif j== 2 and k ==29: #刚好在闰年闰天
day = sum(month)+1+k else:
day = sum(month)+k
else: #平年
month = month[:j-1]
day = sum(month)+k print('这一天是这一年的第{}天'.format(day))
#!/usr/bin/env python
#-*- coding:utf-8 -*- import calendar
year = int(input("year: "))
month = int(input("month: "))
day = int(input("day: "))
days = 0
if 0 < month <= 12:
_, month_day = calendar.monthrange(year, month)
if day <= month_day:
for m in range(1, month):
_, month_day = calendar.monthrange(year, m)
days += month_day
days += day
print days
else:
print "input day error"
else:
print "input month error"
通过计算输入的日期与相应年份1月1日相差的秒数,然后除以每天的秒数3600*24,即可得到输入日期的天数:
#!/usr/bin/env python3 import time def datecount(date):
date0=date[0:4]+"-01-01"
datet=time.strptime(date,"%Y-%m-%d") #将输入的字符串转化为时间元组
date0t=time.strptime(date0,"%Y-%m-%d")
dates=time.mktime(datet) #将时间元组转化为时间戳
date0s=time.mktime(date0t)
count=(dates-date0s)/(3600*24) #输入日期的时间戳减当前年份0101的时间戳除以每天秒数
return count+1 a=input("请输入日期:格式如2017-06-16\n")
print("{}是{}年第{}天".format(a,a[0:4],int(datecount(a))))
考虑实际的情况,比如输入月份为13月或输入天数为65天时候报错(日期仅校对0-31天,未按照实际日期校对):
#!/usr/bin/env python
#-*- coding:utf-8 -*- print('输入年月日以查看某一日期是当年第几天\n')
year = int(input('请输入年份:\n'))
month = int(input('请输入月份:\n'))
day = int(input('请输入日期:\n'))
months = [31,28,31,30,31,30,31,31,30,31,30,31]
d = 0 if 0<month<=12:
if 0<day<=31:
d = d + day
if month > 2:
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
d = d + 1
for i in range(12):
if (i+1) < month:
d = d + months[i]
i = i + 1
print(d)
else:
print('输入的日期有错误')
else:
print('输入的月份有错误')
通过输入时间点的unix时间戳和输入年份首日的Unix时间戳之间的差,来计算经过的时间:
#coding=utf-8
import time
print "Please Enter full number just like 02 01 03"
y = int(raw_input('Enter the year:')) #分别输入年月日
m = int(raw_input('Enter the month:'))
d = int(raw_input('Enter the day:'))
a = (y,m,d,00,00,00,00,00,00) #要求长度为9
b = (y,01,01,00,00,00,00,00,00) #输入年份的第一天
timestampa = time.mktime(a) #两个都转换为Unix时间戳,即1970.1.1到现在经过的秒数
timestampb = time.mktime(b)
timec = int((timestampa - timestampb)/(3600*24)) #输入的时间戳减去年份首天的时间戳等于经过的秒数,再换算成天,取整
print("There are {} days goes by!".format(timec))
python3 利用time模块,简洁写法:
import time
print(time.strptime('2017-9-20', '%Y-%m-%d')[7])
Python2.x 与 Python3.x 兼容:
#!/usr/bin/python
# -*- coding: UTF-8 -*- # 觉得自己的逻辑看起来更顺眼,嘻嘻!
days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
yy = int(input('请输入年份:'))
if yy >= 0:
mm = int(input('请输入月份:'))
if 0 < mm < 13:
dd = int(input('请输入日期:'))
if ((yy % 4 == 0) and (yy % 100 != 0)) or (yy % 400 == 0):
if mm <= 2:
sum = days[mm - 1] + dd
else:
sum = days[mm - 1] + 1 + dd
else:
sum = days[mm - 1] + dd
print('您输入的时间在这一年的第%d天' % sum)
else:
print('您输入的月分不正确')
else:
print('请输入正确的公元年')
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from functools import reduce year = int(input('请输入年(如:2017):'))
month = int(input('请输入月(如:3):'))
day = int(input('请输入日(如:16):')) mday = [0,31,28 if year%4 else 29 if year%100 else 28 if year%4 else 29,31,30,31,30,31,31,30,31,30,31] print('{}年{}月{}日是当年的第{}天'.format(year, month, day, reduce(lambda x,y:x+y, mday[:month])+day))
加入异常处理,确保日期输入格式正确:
import time while 1:
try:
a=input('请输入日期yyyy-mm-dd:')
b=time.strptime(a,'%Y-%m-%d') #按输入格式转化成时间格式 local变量
except ValueError:
print('请输入正确的日期格式!')
else:
b=time.strptime(a,'%Y-%m-%d') #按输入格式转化成时间格式 global变量
dd=time.strftime('%j',b) #返回一年中的第几天
yy=time.strftime('%Y',b) #返回年份
print('输入的日期是%s年的第%s天'%(yy,dd))
break
基本上就是各种各样的案例。。。可见诸位的大才。。。
如果感觉不错的话,请多多点赞支持哦。。。
原文链接:https://blog.csdn.net/luyaran/article/details/80016648
python2.7练习小例子(四)的更多相关文章
- python2.7练习小例子(二十四)
24):1.题目:利用递归方法求5!. 程序分析:递归公式:fn=fn_1*4! #!/usr/bin/python # -*- coding: UTF-8 -*- def fact( ...
- python2.7练习小例子(二十九)
29):1.题目:按相反的顺序输出列表的值. #!/usr/bin/python # -*- coding: UTF-8 -*- a = ['one', 'two', 'three'] for ...
- python2.7练习小例子(十二)
12):题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数" ...
- python2.7练习小例子(八)
8):题目:输出 9*9 乘法口诀表. 程序分析:分行与列考虑,共9行9列,i控制行,j控制列. 程序源代码: #!/usr/bin/python # -*- coding: ...
- python2.7练习小例子(七)
7):题目:将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:]. 程序源代码: #!/usr/bin/python # -*- coding: UTF-8 -*- ...
- python2.7练习小例子(二十七)
27):题目:一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. #!/usr/bin/python # -*- coding: UTF-8 -* ...
- python2.7练习小例子(二十八)
28):题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. ...
- python2.7练习小例子(二十三)
23):题目:求1+2!+3!+...+20!的和. 程序分析:此程序只是把累加变成了累乘. #!/usr/bin/python # -*- coding: UTF-8 -*- n = ...
- python2.7练习小例子(十八)
19):题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. #!/usr/bin/python # -*- ...
- python2.7练习小例子(十七)
17):题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制. 程序分析: ...
随机推荐
- ZT acct 中文man页面(1)
acct 中文man页面(1) 2011-08-18 13:57 佚名 博客转载 我要评论(0) 字号:T | T 如果在内核编译时开启了进程记账选项(CONFIG_BSD_PROCESS_ACCT) ...
- android Listview 软引用SoftReference异步加载图片
首先说一下,android系统加载大量图片系统内存溢出的3中解决方法: (1)从网络或本地加载图片的时候,只加载缩略图.这个方法的确能够少占用不少内存,可是它的致命的缺点就是,因为加载的是缩略图,所以 ...
- IOS JPush 集成步骤(极光远程推送解决方案,支持android和iOS两个平台)
● 什么是JPush ● 一套远程推送解决方案,支持android和iOS两个平台 ● 它能够快捷地为iOS App增加推送功能,减少集成APNs需要的工作量.开发复杂 度 ● 更多的信息,可 ...
- UVALive 6261 Jewel heist
题意:珠宝大盗Arsen Lupin偷珠宝.在展厅内,每颗珠宝有个一个坐标为(xi,yi)和颜色ci. Arsen Lupin发明了一种设备,可以抓取平行x轴的一条线段下的所有珠宝而不触发警报, 唯一 ...
- 【[SDOI2016]排列计数】
一眼题,答案就是\(C_m^m*d_{n-m}\) 就是从\(n\)个中选取\(m\)个在位,剩下的错排,之后就是乘法原理了 但是我发现我的错排公式竟然一直不会推 这个递推式很简单,就是\(d[1]= ...
- 【转】JS模块化工具requirejs教程(二):基本知识
前一篇:JS模块化工具requirejs教程(一):初识requirejs 我们以非常简单的方式引入了requirejs,这一篇将讲述一下requirejs中的一些基本知识,包括API使用方式等. 基 ...
- PS中会使用到的快捷键有那些?
P.S:我刚刚在百度上,搜了一些关于PS的快捷键的使用. 我把它总结了一下.对我今后的P图有所帮助. PS的所有快捷键 1. 显示/隐藏选择区域 [Ctrl]+[H] 2. 取消当前命令:Esc: 工 ...
- 【洛谷P4342】[IOI1998]Polygon
Polygon 比较裸的环形DP(也可以说是区间DP) 将环拆成链,复制到后面,做区间DP即可 #include<iostream> #include<cstdio> usin ...
- 自动化测试selenium教程
什么是自动化测试: 自动帮我们测试一个系统里面的主要功能,一个app.电脑网站.网页,每个系里面许多的功能,好比一个淘宝页面,里面N多功能,登录.注册,推荐,商品详情.评论等等:软件生命周期:需求调研 ...
- JavaFXML实现新窗口打开
实现原理顺着往下看就明白了,流程看红色字体.具体还有什么问题可以留言. 主页面配置文件,一共三个按钮.这里说明第一个按钮触发打开新窗口 <?xml version="1.0" ...