python2.7练习小例子(四)

2018-06-18 02:36:18来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

    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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python学习第七天——随机码生成

下一篇:使用IPython调试代码