葑纆
发表于 2018-10-23 17:35:38
A = 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)))
def Year():
x = input("请输入几年:")
#判断是否是正确年份
if not str(x).isdigit():
print("请输入正确年份")
return Year()
else:
return int(x)
def Month():
y = input("请输入几月:")
#判断是否是正确年份
if not str(y).isdigit() or int(y) > 12:
print("请输入正确月份")
return Month()
else:
return int(y)
def Day():
z = input("请输入几日:")
#判断是否是正确年份
if not str(z).isdigit()or int(z) > A:
print("请输入正确日数")
return Day()
else:
return int(z)
def Num():
num_days = z
if y == 1:
return num_days
else:
for num in range(1,y):
num_days += A
#判断是否是闰年
if ((x % 4 == 0 and x % 100 !=0)or x % 400 ==0) and y > 2:
num_days += 0.5
return num_days
if __name__ == '__main__':
x = Year()
y = Month()
z = Day()
num_days = Num()
print("该天是%d年的第%d天" % (x,num_days))
我这个写的很繁琐,不过就是在判断是否是闰年的那里我弄不明白为什么会乘2(原本是 +=1的,然后如果是闰年的话就会多一天,变成0.5的话就是正常的天数)
wer5zr
发表于 2018-10-25 11:26:42
本帖最后由 wer5zr 于 2018-10-25 11:29 编辑
import datetime
dd = input("Please enter date (e.g. 20180101):")
dd = datetime.datetime.strptime(dd, "%Y%m%d")
print(dd.timetuple().tm_yday)
I_love_fishc_tj
发表于 2018-10-27 14:36:06
源稚空 发表于 2017-4-17 21:20
year = int(input('year = '))
month = int(input('month = '))
day= int(input('day = '))
year % 100 != 0 要改成 year % 100 == 0吧?
可爱的小本子
发表于 2018-10-27 17:31:25
jerryxjr1220 发表于 2017-3-26 12:02
是随便输入日期啊,只要减前一年的12月31不就是当年的第几天?
666
三水吉
发表于 2018-11-19 17:11:31
参考大佬的答案后发一个自己写的:
import datetime as dt
year = int(input('请输入年:'))
month = int(input('请输入月:'))
day = int(input('请输入日:'))
s = dt.datetime(year,month,day) - dt.datetime(year-1,12,31)
print('你输入的日期是当年的第%s天'% s.days)
笑笑!
发表于 2018-11-28 00:31:01
新手·ing 发表于 2017-3-26 07:37
我的解答!
months = (0,31,59,90,120,151,181,212,243,273,304,334)大哥,能不能说一下这个什么意思啊?
笑笑!
发表于 2018-11-28 00:31:47
新手·ing 发表于 2017-3-26 07:37
我的解答!
months = (0,31,59,90,120,151,181,212,243,273,304,334)大哥能不能说一下这个什么意思啊
double-lee
发表于 2018-12-3 21:16:01
count = 0
month =
try:
y,m,d = input('请输入年月日(以.号隔开,例2018.1.2): ').split('.')
y = int(y)
m = int(m)
d = int(d)
except:
print("输入错误")
else:
for i in range(m):
count += month
count += d
if y % 400 == 0 or ( y % 4 == 0 and y % 100 != 0):
if m > 2:
count += 1
print("%d年%d月%d日是这一年的第%d天"%(y,m,d,count))
晓程序
发表于 2018-12-10 17:29:27
def ts(n=eval(input("year:")),y=eval(input("month:")),r = eval(input("day:"))):
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()
这是对十三楼的一点改动
nnxiaod
发表于 2018-12-31 17:05:24
本帖最后由 nnxiaod 于 2018-12-31 17:09 编辑
def do1(y, m, d):
days =
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
days = 29
numbers = d
for i in range(m - 1):
numbers += days
print(numbers)
if __name__ == '__main__':
y = int(input("year:"))
m = int(input("month:"))
d = int(input("day:"))
do1(y, m, d)
lwy520
发表于 2019-3-9 13:43:26
import datetime
def shuchu(year=int(datetime.datetime.now().year),
month = int(datetime.datetime.now().month),
day = int(datetime.datetime.now().day)):
danum1=
danum2=
if year % 100:
if year % 4:
for each in danum1:
day += each
else:
for each in danum2:
day += each
else:
if year % 400:
for each in danum1:
day += each
else:
for each in danum2:
day += each
return day
strday=input("请输入年月日(格式:****-**-**,不输入以空格表示,取现在时间):")
strday_sp=strday.split('-')
if strday_sp != ' ':
year=int(strday_sp)
else:
year=datetime.datetime.now().year
if strday_sp != ' ':
month=int(strday_sp)
else:
month = datetime.datetime.now().month
if strday_sp != ' ':
day=int(strday_sp)
else:
day = datetime.datetime.now().day
day_out=shuchu(year,month,day)
print('这是这一年的'+str(day_out) + '天')
新手zz
发表于 2019-3-10 11:56:59
import datetime
stard=(input("请输入开始的时间:"))
end=(input("请输入结束的时间:"))
"""日期输入格式为2017-3-4"""
a=datetime.datetime(stard)
b=datetime.datetime(end)
c=(a-b)
e=datetime.datetime.datetime(b).day()
c=(a-b).seconds
print("%d年的第%d天"%(e,c)).days
有大佬帮忙指导一下我这个错的原因以及修正吗?
yu123py
发表于 2019-4-26 16:43:57
y, m, d = input('please enter date:').split()
mping =
mrun=
y, m, d = int(y), int(m), int(d)
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
sumday = sum(mrun[:m-1]) + d
print('it is the number {} day of the year.'. format(sumday))
else:
sumday = sum(mping[:m-1]) + d
print('it is the number {} day of the year.'. format(sumday))
小猪猪妍
发表于 2019-7-4 09:55:33
a=input('请输入年份:')
b=input('请输入月份:')
c=input('请输入日子:')
a=int(a)
b=int(b)
c=int(c)
if a/4-int(a/4)==0: #闰年,2月是29天
d=(b-1)*30+c
if b==4 or b==5:
print(d+1)
elif b==6 or b==7:
print(d+2)
elif b==8:
print(d+3)
elif b==9 or b==10:
print(d+4)
elif b==11 or b==12:
print(d+5)
else:
print(d)
else:
if b<=2:
d=(b-1)*31+c
print(d)
else:
d=(b-1)*30+c-1
if b==4 or b==5:
print(d+1)
elif b==6 or b==7:
print(d+2)
elif b==8:
print(d+3)
elif b==9 or b==10:
print(d+4)
elif b==11 or b==12:
print(d+5)
else:
print(d)
糠爸
发表于 2019-7-7 11:35:05
为了破解输入错误,使用了While循环,但是绕不出来了,如果连续错误,忽略最后的数字,可以解答,请各位大佬指教!
#题目:输入某年某月某日,判断这一天是这一年的第几天?
#程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
while month > 12 or month <= 0:
print("你是外星人吗?你家每年不是12个月,请重新输入!")
month = int(input('请输入正确月份:'))
day = int(input('请输入日期:'))
leap_year =
not_leap_year =
temp = 0
while temp <= 3:
if day <= 0:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif day > 31:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif0 < day <= 31:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
t = int(leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
else:
t = int(not_leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
if temp == 3:
print("被你玩坏了!再见!")
break
numbers = 0
times = 1
while times <= month:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
for i in leap_year[:month]:
numbers = numbers + i
times += 1
else:
for i in not_leap_year[:month]:
numbers = numbers + i
times += 1
numbers = numbers + day
print(numbers)
糠爸
发表于 2019-7-7 11:35:38
本帖最后由 糠爸 于 2019-7-7 11:37 编辑
只学到列表!
为了规避连续的输入错误,使用了wehile循环,导致绕不出来了,如果连续错误,需要忽略最后的数字!
请各位专家指教,我的思路还需要怎样改!
#题目:输入某年某月某日,判断这一天是这一年的第几天?
#程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
while month > 12 or month <= 0:
print("你是外星人吗?你家每年不是12个月,请重新输入!")
month = int(input('请输入正确月份:'))
day = int(input('请输入日期:'))
leap_year =
not_leap_year =
temp = 0
while temp <= 3:
if day <= 0:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif day > 31:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif0 < day <= 31:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
t = int(leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
else:
t = int(not_leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
if temp == 3:
print("被你玩坏了!再见!")
break
numbers = 0
times = 1
while times <= month:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
for i in leap_year[:month]:
numbers = numbers + i
times += 1
else:
for i in not_leap_year[:month]:
numbers = numbers + i
times += 1
numbers = numbers + day
print(numbers)
糠爸
发表于 2019-7-7 11:53:12
#题目:输入某年某月某日,判断这一天是这一年的第几天?
#程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
while month > 12 or month <= 0:
print("你是外星人吗?你家每年不是12个月,请重新输入!")
month = int(input('请输入正确月份:'))
day = int(input('请输入日期:'))
leap_year =
not_leap_year =
temp = 0
while temp <= 3:
if day <= 0:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif day > 31:
print("学习呢!请输入正确日期!")
day = int(input("请输入正确日期:"))
temp += 1
elif0 < day <= 31:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
t = int(leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
else:
t = int(not_leap_year)
if day > t:
print("这是地球,请输入准确日期!")
day = int(input("请输入正确日期:"))
temp += 1
break
if temp == 3 and day > t:
print("被你玩坏了!再见!")
__exit__
numbers = 0
times = 1
while times <= month:
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
for i in leap_year[:month]:
numbers = numbers + i
times += 1
else:
for i in not_leap_year[:month]:
numbers = numbers + i
times += 1
numbers = numbers + day
print("看,机智如我"+str(year)+ "-"+str(month)+"-"+str(day)+"是"+str(year)+"的第"+str(numbers)+"天!")
panheng
发表于 2019-8-6 09:48:42
本帖最后由 panheng 于 2019-8-6 10:03 编辑
先交作业,再看其他答案
import datetime
#解法1: 首先获取日期,累加计算常年天数,判断该年是否为闰年,是否加一天。
def answer1():
days = getDays(month,day) #调用getDay函数计算天数
if leap_year(year): #调用leap_year函数判断闰年
if month >2: #剔除掉日期为2月29日前计算的闰年天数
days += 1
print(days)
def getDays(month,day): #计算天数
days = 0 #初始化天数0
list1 = #逐月天数
for each in range(month-1):
days += list1
days += day
return days
def leap_year(year = 1000): #判断是否为闰年
if year % 4 == 0 and year % 100 != 0:
return True
elif year % 400 == 0:
return True
else:
return False
#解法2:使用datetime模块,计算日期差值。输出结果为 *days, 0:00:00时间格式
def answer2():
date1 = datetime.date(year,month,day) #设定目标日期
date2 = datetime.date(year-1,12,31) #设定计算起始日期,由于1月1日不包含当天,故日期设定为上年最后1天。
days = date1-date2
print(days)
#解法3:使用datetime模块,使用timetuple方法获取日期时间属性,输出当年tm_yday参数即可(元组序列正序第8或倒数第二)
def answer3():
date = datetime.date(year,month,day)
print(date.timetuple()[-2])
if __name__ == "__main__":
date = input("请输入日期(格式:年/月/日):")
# 获取年月日
year = int(date.split("/"))
month = int(date.split("/"))
day = int(date.split("/"))
answer1()
answer2()
answer3()
wangxiaofeng
发表于 2019-8-19 21:14:49
x=input("输入某年某月某日=(如\"0022年02月02日\")")
year=int(x)
month=int(x)
day=int(x)
demo1=
demo2=
def isrun():
if (year%4==0 and year%100!=0) or year%400==0:
return demo1
else:
return demo2
days=(sum(isrun())+day)
print(days)
克里斯保罗
发表于 2019-8-25 11:55:49
交作业~ 等会用datetime再做一遍~