Python:每日一题 4
题目:输入某年某月某日,判断这一天是这一年的第几天?程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
欢迎小伙伴们,一起答题!
如果你有能力,欢迎加入我们!
已经上车老司机:@ooxx7788
点我上车{:10_298:} def leapyear(n):
return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False
days =
year, month, day =
day2 = sum(days[:month - 1]) + day
if leapyear(year) and month > 2:
day2 += 1
print(day2) datetime不是可以直接计算的?算两个日期间的差
import datetime as dt
print dt.datetime(2017,3,5)-dt.datetime(2016,12,31) 冬雪雪冬 发表于 2017-3-25 23:04
感谢版主支持! jerryxjr1220 发表于 2017-3-25 23:27
datetime不是可以直接计算的?算两个日期间的差
import datetime as dt
print dt.datetime(2017,3,5)-dt. ...
不是的
是随便输入日期,判断 year = int(input('year:'))
month = int(input('month:'))
day = int(input('day:'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month and month <= 12:
sum = months
else:
print('亲,一年只有12个月!')
sum += day
l = 0
if year % 4 == 0 or year % 400 == 0:
l = 1
if l == 1 and month > 2:
sum += 1
print('这是 %s 年的第 %s 天!' % (year,sum))
我的解答! 是随便输入日期啊,只要减前一年的12月31不就是当年的第几天? jerryxjr1220 发表于 2017-3-26 12:02
是随便输入日期啊,只要减前一年的12月31不就是当年的第几天?
{:10_245:}大佬... 新手·ing 发表于 2017-3-26 12:32
大佬...
python的强大就在于有非常多好用的库,有现成的解决方案就不要重复造轮子了 jerryxjr1220 发表于 2017-3-26 12:55
python的强大就在于有非常多好用的库,有现成的解决方案就不要重复造轮子了
学习了{:10_245:} 我只知道datetime能计算,但是不熟,查了半天,写了个:
def test4(date):
'''日期的输入方式使用2017-4-1这样的'''
the_date = datetime.datetime.strptime(date,"%Y-%m-%d")
the_year = datetime.datetime.strftime(the_date,"%Y")
start_date = str(the_year)+"-1-1"
start_date = datetime.datetime.strptime(start_date,"%Y-%m-%d")
dates = the_date - start_date + datetime.timedelta(days=1)
print (dates)
test4('2017-4-1')
>>>91 days, 0:00:00 {:5_100:},不用模板的话闰年要判断,填写的月份要判断,填写的日子又要判断,例如,填写的4月31号,四月没有31号{:5_100:}想了好久。。感觉我写的好丑好长 def ts(n=2017,y=4,r=11):
day=0
ey=29 if n%4==0 and n%100!=0 else 28
for i in range(1,y):
if i in: day+=31
elif i==2: day+=ey
else : day+=30
print('%d月%d号是%d年的第%d天'%(y,r,n,(day+r)))
ts() year = int(input('year = '))
month = int(input('month = '))
day= int(input('day = '))
today =
if year % 4 == 0 or (year % 400 == 0 and year%100!=0):
today = 29
i = 0
d = 0
for i in range(month) :
d = d + today
i = i + 1
print(d) 本帖最后由 seasonjj 于 2017-4-19 11:32 编辑
day = str(input('输入年月日(列:20170419):'))
y = int(day[:4])
m = int(day)
d = int(day)
md = int(day)
mlist =
i = list(range(m))
days = 0
if y%400 == 0 or (y%4 == 0 and y%100 != 0):
if md > 228:
days = 1
else:
days = 0
for each in i:
days += mlist
days += d
print(days)
else:
for each in i:
days += mlist
days += d
print(days)
好臭好长的感觉
{:5_107:} 看起来大家的想法差别不大,基本编程还没学会,库什么的还是留后面吧,记得有这么回事就是了
years = {'common_year': ,\
'leap_year': }
judge = lambda y: 'leap_year' if ((y%4==0 and y%100 !=0) or y%400==0) else 'common_year'
try:
year = int(input("请输入年份(正整数):"))
month = int(input("请输入月份(正整数1-12):"))
day = int(input("请输入日期(正整数):"))
months = years # 年份判断
if year>=0 and 12>=month>0 and months>=day>0:
num = sum(months[:month-1])+ day
print("这是%d年第 %d 天" %(year, num))
else:
print("输入超出合理范围,请重新输入")
except ValueError:
print("输入格式有误")
本帖最后由 渡漫 于 2017-5-23 22:51 编辑
year=int(input('输入年份:'))
mouth=int(input('输入月份:'))
day=int(input('输入日期:'))
sumDay=0 #总天数即第几天
isLeapYear=False
perDay= #以列表的方式存储每个月的天数
if (year%4==0 and year%100!=0) or year%400==0:#是否为闰年
isLeapYear=True
for i in range(mouth): #以循环的方式叠加天数
sumDay+=perDay
if isLeapYear==True and mouth>2:
sumDay+=1
sumDay+=day
print('这是一年中的第{0}天'.format(sumDay))
看了一下楼上的大佬,月份判断,日期判断。。。好烦,果然我还是太年轻了 print("请输入某年某月某日")
a=int(input())
b=int(input())
c=int(input())
day =
m=0
result=0
if((a%4==0 and a%100!=0) or a%400==0):
m=1
for n in range(1,b):
result=result+day
result=result+m+c
print ('这是一年的第%d天' %result )
代码多了点,但是排除了所有输入错误的情况,所有输入错的数值都要重新输入
a = input ("please enter the year:")
while a.isdigit()!= True:
a = input ("ERROR! Please enter the year again:")
yy = int (a)
if yy % 4 != 0:
r = 1
elif (yy % 400 == 0) or (yy % 100 != 0):
r = 2
elif yy % 100 == 0:
r = 1
b = input ("please enter the month:")
b1 = ['1','2','3','4','5','6','7','8','9','10','11','12']
while b not in b1:
b = input ("ERROR! please enter the month again:")
mm = int (b)
if r == 1:
month =
else:
month =
day = month
i = 1
day1 = []
while i<=day:
temp = str(i)
day1.append (temp)
i += 1
c = input ("please enter the day:")
while c not in day1:
c = input ("ERROR! please enter the day again:")
dd = int (c)
n = 0
while mm != 1:
n += month
mm -= 1
n += dd
print (n) def fuction1(a):
#该函数判断是否为闰年,是返回1,否则返回0.
if(a%4 ==0 and a%100 != 0) or a % 400 == 0:
return 1
return 0
def fuction2(riqi):
#通过年月日计算第几天
runnian = fuction1(riqi)
if riqi == 1:
return riqi
elif riqi == 2:
return 31 + riqi
elif riqi == 3:
return runnian + 59 + riqi
elif riqi == 4:
return runnian + 90 + riqi
elif riqi == 5:
return runnian + riqi + 120
elif riqi == 6:
return runnian + riqi + 151
elif riqi == 7:
return runnian + riqi + 181
elif riqi == 8:
return runnian + riqi + 212
elif riqi == 9:
return runnian + riqi + 243
elif riqi == 10:
return runnian + riqi + 273
elif riqi == 11:
return runnian + riqi + 304
elif riqi == 12:
return runnian + riqi + 334
def fuction3(riqi):
#用户输入年月日
riqi = int(input('请输入年份,并按回车结束,输入0可结束程序'))
if riqi != 0:
riqi = int(input('再输入月份,并按回车结束'))
riqi = int(input('最后输入几号,并按回车结束'))
'''
fuction3的测试程序
riqi =
fuction3(riqi)
print(riqi)
'''
riqi =
while riqi:
fuction3(riqi)
if riqi == 0:
break
riqi = fuction2(riqi)
print('您输入的日期是该年的第',riqi,'天','\n')