輸入某年某月某日吓妆,判斷這一天是這一年的第幾天。(格式 如:19870604)
from builtins import len
leap_year={'01':31,'02':29,'03':31,'04':30,'05':31,'06':30,
'07':31,'08':31,'09':30,'10':31,'11':30,'12':31} #閏年每月最大天數(shù)
comm_year={'01':31,'02':28,'03':31,'04':30,'05':31,'06':30,
'07':31,'08':31,'09':30,'10':31,'11':30,'12':31} #平年每月最大天數(shù)
while True:
num = input('請輸入日期吨铸,格式如19540105:')
if num.isdigit() and len(num)==8 :
pass
else :
print('格式錯誤行拢,請重新輸入')
continue
year_num = int(num[:4])
month_num = num[4:6]
day_num = int(num[6:])
if (int(month_num)>12) or (int(month_num)<=0): #月份錯誤時,重新輸入,'01'字符串轉(zhuǎn)換為整數(shù)時诞吱,會舍棄0舟奠,無法遍歷字典
print('月份輸入錯誤,請重新輸入房维。')
continue
days=0 #定義天數(shù)
if ((year_num%100==0) and (year_num%400 == 0)) or ((year_num%100 !=0) and (year_num%4 == 0)): #如果是閏年
if (day_num > leap_year[month_num]) or (day_num <= 0): #日期錯誤時重新輸入
print('此月份中沒有這一天沼瘫,請重新輸入。')
continue
for i in leap_year.keys(): #遍歷月份
if int(i) < int(month_num):
days = days + leap_year[i]
elif int(i)==int(month_num):
days = days + day_num
else: #如果是平年
if (day_num > comm_year[month_num]) or (day_num <= 0): #日期錯誤時重新輸入
print('此月份中沒有這一天咙俩,請重新輸入耿戚。')
continue
for i in comm_year.keys(): #遍歷月份
if int(i) < int(month_num):
days = days + comm_year[i]
elif int(i)==int(month_num):
days = days + day_num
print('這一天是今年的第 %d 天' %days)