【1】寫一個函數,計算給定日期是該年的第幾天.


 1 #coding=utf-8
 2 #寫一個函數,計算給定日期是該年的第幾天.
 3 
 4 
 5 def count(year,month,day):
 6     count = 0
 7     #判斷該年是平年還是閏年
 8     if year%400==0 or (year%4==0 and year%100!=0):
 9         print('%d年是閏年,2月份有29天!'%year)
10         li1 = [31,29,31,30,31,30,31,31,30,31,30,31]
11         for i in range(month-1): 12             count += li1[i]
13         return count+day
14     else:
15         print('%d年是平年,2月份有28天!' % year)
16         li2 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
17         for i in range(month-1):
18             count += li2[i]
19         return count+day
20 
21 
22 if __name__ == "__main__":
23     year = int(input('請輸入年份:'))
24     month = int(input('請輸入月份:'))
25     day = int(input('請輸入日期:'))
26     count = count(year,month,day)
27     print('%d年%d月%d日是今年的第%d天!'%(year,month,day,count))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM