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))
来源:oschina
链接:https://my.oschina.net/u/4293290/blog/3424073