【python】 代码哪里出问题了
提交代码显示答案错误
题目:
输入:输入共一行,两个整数,代表年和月,中间用空格隔开。
输出:一个整数,代表这一年这个月有几天。
样例输入:2017 1
样例输出:31
注:1 3 5 7 8 10 12月有31天 4 6 9 11月有30天 闰年的2月有29天,平年的2月有28天。
i = input()
year = 0
number = 0
for k in i:
if k != ' ':
year = year * 10 + int(k)
number = number + 1
else:
month = int(i[(number+1):])
break
if ((month == 4) or (month == 6) or (month == 9) or (month == 11)):
print("30")
else:
if month != 2:
print("31")
else:
if (year % 4) != 0:
print("28")
else:
print("29")
你对大小月和闰年的判断,都是错的
import calendar
year, month = map(int, input().split())
days = calendar.monthrange(year, month)[1]
print(days)
或者自己写闰年判断
year, month = map(int, input().split())
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
print(days[month-1])