判斷當前的輸入為當年的第幾天

2021-10-01 02:53:36 字數 3891 閱讀 6274

**經過測試,個人感覺沒有什麼問題,但由於個人的經驗不足以及眼光不夠,希望各位指正程式中的不足之處。

附上**

def check():

'''輸入乙個日期,格式為***x-xx-xx,判斷這一天為當年的第幾天

'''print('輸入乙個日期,格式為(***x-xx-xx):', end='')

data = input()

if '-' not in data or data.count('-') != 2:

print('請按照正確格式輸入年月日')

return check()

else:

str1 = data.split('-')

year = int(str1[0]) # 年

month = int(str1[1]) # 月

day = int(str1[2]) # 日

if month < 1 or month > 13:

print('月輸入錯誤,請重新輸入')

return check()

elif month in day31: # 月的天數為31天

if day > 31 or day == 0:

print('日輸入錯誤,該月只有31天,請重新輸入')

return check()

else:

if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判斷是否為閏年

print(year366[month - 1] + day)

else:

print(year365[month - 1] + day)

elif month in day30: # 月的天數為30天

if day > 30 or day == 0:

print('日輸入錯誤,該月只有30天,請重新輸入')

return check()

else:

if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判斷是否為閏年

print(year366[month - 1] + day)

else:

print(year365[month - 1] + day)

elif month == 2:

if ((day <= 28 or (year % 4 == 0 and year % 100 != 0 and day < 29) or (year % 400 == 0 and day <= 29))):

if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0: # 判斷是否為閏年

print(year366[month - 1]+day)

else:

print(year365[month - 1]+day)

else:

print('日輸入錯誤,2月最多29天,或者該年不是閏年,請重新輸入')

return check()

if __name__ == '__main__':

day31 = [1, 3, 5, 7, 8, 10, 12] #天數為31天的月份

day30 = [4, 6, 9, 11] #天數為30天的月份

year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365] #平年每個月結束時的天數 365天

year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] #閏年每個月結束時的天數 366天(2月多一天)

check()

def check(

): ''

' 輸入乙個日期,格式為***x-xx-xx,判斷這一天為當年的第幾天

''' print(

'輸入乙個日期,格式為(***x-xx-xx):', end='')

data = input()if

'-' not in data or data.count(

'-')

!= 2:

print(

'請按照正確格式輸入年月日'

)return check(

) else:

str1 = data.split(

'-')

year = int(str1[0]

)# 年

month = int(str1[1]

)# 月

day = int(str1[2]

)# 日

if month < 1 or month > 13:

print(

'月輸入錯誤,請重新輸入'

)return check(

)elif month in day31: # 月的天數為31天

if day > 31 or day == 0:

print(

'日輸入錯誤,該月只有31天,請重新輸入'

)return check(

) else:

if year % 4 == 0 :

# 判斷是否為閏年

print(year366[month - 1] + day)

else:

print(year365[month - 1] + day)

elif month in day30: # 月的天數為30天

if day > 30 or day == 0:

print(

'日輸入錯誤,該月只有30天,請重新輸入'

)return check(

) else:

if year % 4 == 0 :

# 判斷是否為閏年

print(year366[month - 1] + day)

else:

print(year365[month - 1] + day)

elif month == 2:

if((day <=

28 or (year %4!=

0 and day <

29) or (year %4==

0 and day <=

29))

): if year % 4 == 0 :

# 判斷是否為閏年

print(year366[month - 1]+day)

else:

print(year365[month - 1]+day)

else:

print(

'日輸入錯誤,2月最多29天,或者該年不是閏年,請重新輸入'

)return check(

)if __name__ ==

'__main__'

: day31 =

[1, 3, 5, 7, 8, 10, 12]

#天數為31天的月份

day30 =

[4, 6, 9, 11]

#天數為30天的月份

year365 =

[0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]

#平年每個月結束時的天數 365天

year366 =

[0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]

#閏年每個月結束時的天數 366天(2月多一天)

check(

)

計算輸入的是當年的第幾天

題目 輸入某年某月某日,判斷這一天是這一年的第幾天?1.程式分析 以3月5日為例,應該先把前兩個月的加起來,然後再加上5天即本年的第幾天,特殊 情況,閏年且輸入月份大於3時需考慮多加一天。1 include 2 include 34 int main 5 18 else 19 2223 計算基本的天...

python判斷輸入日期為第幾天的例項

如下所示 coding utf 8 簡述 要求輸入某年某月某日 提問 求判斷輸入日期是當年中的第幾天?def which day year,month,day list 31,28,31,30,31,30,31,31,30,31,30,31 whichday 0 if year 4 0 and ye...

C C 學習 判斷某日是當年第幾天

學期末兩周實訓,老師布置若干題目。其中,第一題為 判斷某日是當年第幾天。問題描述 用結構體表示日期,輸入乙個日期 年 月 日 計算從輸入這年的1月1日到輸入的日期的總天數days並輸出。1.設計結構體 結構體中包含三個成員,分別為整型的年 月 日。struct date 2.日期輸入設計 這裡按照常...