我已经找遍了所有的地方,我不明白为什么我总是得到同样的错误。我读到它与“回归”有关,但对我来说没有意义。你知道吗
Traceback:
File "/tmp/vmuser_mlgewmyusy/main.py", line 47, in daysBetweenDates
new_year,new_month,new_day=nextDay(new_year,new_month,new_day)
TypeError: 'NoneType' object is not iterable
代码:
def nextDay(year, month, day):
if month!=12:
if month==1 or month==3 or month==5 or month==7 or month==8 or month==10:
if day!=31:
return year,month,day+1
else:
return year,month+1,1
elif month==4 or month==6 or month==9 or month==11:
if day!= 30:
return year,month,day+1
else:
return year,month+1,1
elif month==2:
if day!= 28:
return year,month,day+1
else:
return year,month+1,1
elif month==22:
if day!= 29:
return year,month,day+1
else:
return year,month+1,1
else:
if day!= 31:
return year,month,day+1
else:
return year+1,1,1
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
i=0
new_year,new_month,new_day=year1-100,month1-100,day1-100
if year1==year2 and month1==month2 and day1==day2:
return 0
while new_year!=year2 and new_month!=month2 and new_day!=day2:
if i==0:
`new_year,new_month,new_day`=year1+100,month1+100,day1+100
i+=1
new_year,new_month,new_day=nextDay(new_year,new_month,new_day)
return i
# Test routine
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
print 'ANSWER = {}'.format(answer)
print 'RESULT = {}'.format(result)
else:
print "Test case passed!"
test()
每次循环迭代时,我都要给'new\u year,new\u month,new\u day'赋值。你知道吗
我将这两个打印行添加到
daysBetweenDates
函数中:我尝试这个输入
daysBetweenDates(2015,6,1,2014,4,1)
,发现print(new_month)
给出106
,print(nextDay(new_year,new_month,new_day))
给出None
。查看nextDay
的代码块,没有捕获month==106
的条件,因此函数返回Python默认值None
。你知道吗虽然您的值可能不同,但似乎
new_month
不在集合{1,2,3,4,5,6,7,8,9,10,11,22}中,因此函数返回None
,这就是错误的来源。因此,您可以确保始终返回一个元组,也可以调试代码,以了解为什么您得到的输入数据与为其编写代码的数据不同。你知道吗相关问题 更多 >
编程相关推荐