python 根據年份,月份信息顯示此月份天數


 1 # 普通方法
 2 year = int(input('請輸入年份:'))
 3 month = int(input('請輸入月份(1~12):'))
 4 if month == 2:
 5     if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
 6         print('閏年29天')
 7     else:
 8         print('平年28天')
 9 
10 elif month in (4,6,9,11):
11     print('30天')
12 else:
13     print('31天')
 1 # 方法二:函數方法
 2 def y_m(year, month):
 3     '''
 4     根據年份,月份信息顯示此月份天數
 5     :param year: 請輸入年份:
 6     :param month: 請輸入月份(1~12):
 7     :return: 天數
 8     '''
 9     if month >12 or month <= 0:
10         return -1
11     if month == 2:
12         return 29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28
13 
14     if month in (4, 6, 9, 11):
15         return 30
16     else:
17         return 31
18 
19 print(y_m(2004,12))

 


免責聲明!

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



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