Python函数 循环-判断随机输出的一天是这一年中的哪一天

1.编程:日期计算:输入某年某月某日,判断这一天是这一年的第几天?
1).程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第 几
天, 特殊情况,闰年且输入月份大于3时需考虑多加一天。
2).如果输入的月份大于12时提示错误,重新输入
3).如果月份为小月,天数大于30时提示错误,重新输入
4).如果月份为大月,天数大于31时提示错误,重新输入
5).如果年份为平年,2月天数大于28时提示错误,重新输入
6).如果年份为闰年,2月天数大于29时提示错误,重新输入
方法一:

y=int(input('year'))
m=int(input('month'))
d=int(input('day'))
while True:#判断月份大于12或者小于1,共分两种情况:异常月份 正常月份
if m>12 or m<1:
print('输错了月份')
m=int(input('请重新输入月份'))
if 1<=m<=12:
break
while True:#判断a:当月在大月时(2种情况) b:不在大月(异常日期 正常日期)
if m in(1,3,5,7,8,10,12):
if d>31 or d<1:
print('输错了日期')
d=int(input('请重新输入日期'))
if 1<=d<=31:
break
else:
if d > 30 or d < 1:
print('输错了日期')
d = int(input('请重新输入日期'))
if 1 <= d <= 30:
break
while True:#判断a:闰年(2月两种情况:29天和异常日期)b平年(2月28天和异常日期)
if y%4==0 and y%100!=0 or y%100==0 and y%400==0:
if m==2:
if d > 29 or d < 1:
print('输错了日期')
d = int(input('请重新输入日期'))
if 1 <= d <= 29:
break
else:
break
else:
if m==2:
if d>28 or d<1:
print('输错了日期')
d = int(input('请重新输入日期'))
if 1 <= d <= 28:
break
else:
break
#以上仅仅时判断输入条件,下面开始计算
if 1<=m<=12:
for i in range(1,m):#循环累加前几个月的值

if i in(1,3,5,7,8,10,12):#分3种情况:大月,二月(平年28天,闰年29),其他月
d+=31
elif i==2:
if y%100!=0 and y%4==0 or y%100==0 and y%400==0:
d+=29
else:
d+=28
else:
d+=30
print(d)


方法二:
def getdays():
y=int(input('请输入年:'))
runnian=False#闰年默认为假
if y%4==0 and y%100!=0 or y%400==0:
runnian=True#闰年经过判断之后值可能会发生改变。
while True:
m=int(input('请输入月:'))
if m>12 or m<1:
print('月输入的不合法,请重新输入')
continue
else:
break
while True:
d=int(input('请输入日:'))
if m in(1,3,5,7,8,10,12):
if d>31 or d<1:
print('日期输入的不合法,请重新输入!')
continue
else:
break
elif m==2:
if runnian==True:
if d>29 or d<1:
print('日期输入的不正确,请检查!')
continue
else:
break
else:
if d>28 or d<1:
print('日期输入的不正确,请检查!')
continue
else:
break
else:
if d>30 or d<1:
print('输入的日期不合法,请重新输入!')
continue
else:
break
days=0
for i in range(1,m):
if i in(1,3,5,7,8,10,12):
days+=31
elif i==2:
if runnian==True:
days+=29
else:
days+=28
else:
days+=30
days+=d
print('%d年%d月%d日是%d年的第%d天'%(y,m,d,y,days))
# getdays()
方法三:
def getyear():
year = int(input("请输入年份: "))
result = False
if year % 400 ==0 or (year % 4 ==0 and year % 100 != 0):
result = True

return year, result

def getmonth():
while True:
month = int(input("请输入月份: "))
if month < 1 or month > 12:
print("输入错误,请重新输入月份")
continue
else:
return month

def getday(result,month):
while True:
day = int(input('请输入天数: '))
if month in (4,6,9,11):
if day > 30 or day < 1:
print('请重新输入')
continue
elif month == 2:
if result:
if day >29 or day <1:
print('请重新输入')
continue
else:
if day > 28 or day < 1:
print('请重新输入')
continue
else:
if day > 31 or day < 1:
print('请重新输入')
continue

return day

def getdays(result,month,day):
days = 0
for i in range(1, month):
if i in (4, 6, 9, 11):
days += 30
elif i == 2:
if result:
days += 29
else:
days += 28
else:
days += 31

days += day

return days
year, result = getyear()
month = getmonth()
day = getday(result, month)
days = getdays(result,month,day)
print("%d年%d月%d日,是%d年的%d天"%(year,month,day,year,days))
方法四:
def getdays():
y=int(input('请输入年:'))
runnian=False#闰年默认为假
if y%4==0 and y%100!=0 or y%400==0:
runnian=True#闰年经过判断之后值可能会发生改变。
while True:
m=int(input('请输入月:'))
if m>12 or m<1:
print('月输入的不合法,请重新输入')
continue
else:
break
while True:
d=int(input('请输入日:'))
if m in(1,3,5,7,8,10,12):
if d>31 or d<1:
print('日期输入的不合法,请重新输入!')
continue
else:
break
elif m==2:
if runnian==True:
if d>29 or d<1:
print('日期输入的不正确,请检查!')
continue
else:
break
else:
if d>28 or d<1:
print('日期输入的不正确,请检查!')
continue
else:
break
else:
if d>30 or d<1:
print('输入的日期不合法,请重新输入!')
continue
else:
break
days=0
for i in range(1,m):
if i in(1,3,5,7,8,10,12):
days+=31
elif i==2:
if runnian==True:
days+=29
else:
days+=28
else:
days+=30
days+=d
print('%d年%d月%d日是%d年的第%d天'%(y,m,d,y,days))
# getdays()
posted @ 2022-04-13 13:13  认知迭代  阅读(599)  评论(0)    收藏  举报