python测验4_Python 练习实例4

#19

ym

853***877@qq.com

12

参考解法:

#!/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

ym

ym

853***877@qq.com4年前 (2017-04-11)

#18

苹果pai

646***039@qq.com

10

闰年需要同时满足以下条件:

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

苹果pai

苹果pai

646***039@qq.com4年前 (2017-04-13)

#17

流年细雨

758***823@qq.com

8

参考解法:

#!/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)

流年细雨

流年细雨

758***823@qq.com4年前 (2017-04-13)

#16

Atom

tum***@126.com

6

参考解法:

#!/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, '天!'

Atom

Atom

tum***@126.com4年前 (2017-04-14)

#15

老虎头

sha***026@163.com

3

参考解法:#!/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)

老虎头

老虎头

sha***026@163.com4年前 (2017-04-25)

#14

shusihui

511***990@qq.com

7

参考方法:

#!/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

shusihui

shusihui

511***990@qq.com4年前 (2017-04-27)

#13

lqy126

412***942@qq.com

5

参考方案:#!/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')

lqy126

lqy126

412***942@qq.com4年前 (2017-05-05)

#12

初学者

646***860@qq.com

9

参考方法:

#! /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)

初学者

初学者

646***860@qq.com4年前 (2017-05-11)

#11

cc

123***3.123

5

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)

cc

cc

123***3.1234年前 (2017-06-03)

#10

半音节

104***9456@qq.com

5

真正意义上自己思考写出来的第一题,撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。#!/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))

半音节

半音节

104***9456@qq.com3年前 (2017-06-13)

#9

guanerye

lis***nan7@hotmail.com

3

参考方法:

#!/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"

guanerye

guanerye

lis***nan7@hotmail.com3年前 (2017-06-14)

#8

Think-dfrent

iwa***aoiy@live.com

5

通过计算输入的日期与相应年份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))))

Think-dfrent

Think-dfrent

iwa***aoiy@live.com3年前 (2017-06-16)

#7

日向翔阳

wel***y@yeah.net

4

考虑实际的情况,比如输入月份为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

if 0

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('输入的月份有错误')

日向翔阳

日向翔阳

wel***y@yeah.net3年前 (2017-06-28)

#6

AKILLII

616***634@qq.com

0

通过输入时间点的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))

AKILLII

AKILLII

616***634@qq.com3年前 (2017-07-26)

#5

小青与

190***2527@qq.com

6

python3 利用time模块,简洁写法:

import time

print(time.strptime('2017-9-20', '%Y-%m-%d')[7])

小青与

小青与

190***2527@qq.com3年前 (2017-09-20)

#4

阿科.zck

121***125@qq.com

1

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('请输入正确的公元年')

阿科.zck

阿科.zck

121***125@qq.com3年前 (2017-11-14)

#3

Echo

csz***13@163.com

2

分享一下我的答案:#!/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))

Echo

Echo

csz***13@163.com3年前 (2017-12-05)

#2

阿土仔

227***15@qq.com

2

加入异常处理,确保日期输入格式正确:

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

阿土仔

阿土仔

227***15@qq.com3年前 (2017-12-05)

#1

S

894***936@qq.com

8

参考方法:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

year = int(input('年:'))

mon = int(input('月:'))

day = int(input('日:'))

Days = [0,31,28,31,30,31,30,31,31,30,31,30,31]

if (year % 100 != 0 and year % 4 == 0) or (year % 400 == 0):

Days[2] = 29

for each in range(0,mon):

day += Days[each]

print('天数:%d' %day)

S

S

894***936@qq.com3年前 (2018-05-21)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值