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))