我有一个函数“checkAllInOneDirection”,它是一个递归循环。当我跳出循环时,函数返回 1 个数组和 3 个布尔值。最奇怪的是,在递归函数中,当它返回时,它显示所有这些都有值,但我仍然收到“无类型”错误:
这是打印:
Before return : [[0, 0]] False False False False
但是当它返回并且在它异常出来之前,返回的值变成了
After returned : [[0, 0]] True True True True
虽然递归的返回值发生了变化和错误,但仍有一些价值,但我是“无类型”错误的例外?这是错误:
File "C:\xampp\htdocs\ZoneFinding\zoneFinder2D_V2.py", line 127, in handleCheck
finalCatch, forBoo, bakBoo, upBoo, dwnBoo = checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)
TypeError: 'NoneType' object is not iterable
这是调用递归“checkAllInOneDirection”的函数。
def handleCheck(newCatch, locale, subset, width, height, rowCount, columnCount, posToCheck):
forBoo = True; bakBoo =True; upBoo=True; dwnBoo= True; lastOneInSet =0;
while forBoo and bakBoo and upBoo and dwnBoo :
if locale[posToCheck[0]][posToCheck[1]] == 0:
recursiveCount = 0; tempCatch = []; finalCatch =[]
tempCatch.append(posToCheck)
finalCatch, forBoo, bakBoo, upBoo, dwnBoo = checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)
replaceVal(locale, finalCatch)
return False, finalCatch
这是递归函数 checkAllInOneDirection 代码。
def checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, width, height, forBoo, bakBoo, upBoo, dwnBoo):
isItLast = checkLast(forBoo, bakBoo, upBoo, dwnBoo)
if isItLast:
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch=[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
newCatch=[]
print "Before return :", finalCatch, forBoo, bakBoo, upBoo, dwnBoo
return finalCatch, forBoo, bakBoo, upBoo, dwnBoo
for each in range (0, len(tempCatch)):
posToCheck = posToCheckBak = posToCheckUp = posToCheckDwn = [tempCatch[each][0], tempCatch[each][1]]
### Some code to check position forward
newPosForward = checkForward(posToCheck, width)
if newPosForward != False:
tempLocale = locale[newPosForward[0]][newPosForward[1]]
elif newPosForward == False:
tempLocale = 1
if newPosForward != False and tempLocale ==0 and not newPosForward in finalCatch and not newPosForward in newCatch:
forVal = locale[newPosForward[0]][newPosForward[1]]
newCatch.append(newPosForward)
posToCheck = newPosForward
forBoo = True
elif newPosForward == False and tempLocale == 1 and not newPosForward in newCatch:
forBoo = False
### Some code to check position backward
newPosBackward = checkBackward(posToCheckBak)
if newPosBackward != False:
tempLocale = locale[newPosBackward[0]][newPosBackward[1]]
elif newPosBackward == False:
tempLocale = 1
if newPosBackward != False and tempLocale ==0 and not newPosBackward in finalCatch and not newPosBackward in newCatch:
forVal = locale[newPosBackward[0]][newPosBackward[1]]
newCatch.append(newPosBackward)
posToCheckBak = newPosBackward
bakBoo = True
elif newPosBackward == False and tempLocale == 1 and not newPosBackward in newCatch:
bakBoo = False
### Some code to check position upward
newPosUp = checkUpRow(posToCheckUp)
if newPosUp != False:
tempLocale = locale[newPosUp[0]][newPosUp[1]]
elif newPosUp == False:
tempLocale = 1
if newPosUp != False and tempLocale ==0 and not newPosUp in finalCatch and not newPosUp in newCatch:
forVal = locale[newPosUp[0]][newPosUp[1]]
newCatch.append(newPosUp)
posToCheckUp = newPosUp
upBoo = True
elif newPosUp == False and tempLocale == 1 and not newPosUp in newCatch:
upBoo = False
### Some code to check position downward
newPosDwn = checkDwnRow(posToCheckDwn, height)
if newPosDwn != False:
tempLocale = locale[newPosDwn[0]][newPosDwn[1]]
elif newPosDwn == False:
tempLocale = 1
if newPosDwn != False and tempLocale ==0 and not newPosDwn in finalCatch and not newPosDwn in newCatch:
forVal = locale[newPosDwn[0]][newPosDwn[1]]
newCatch.append(newPosDwn)
posToCheckDwn = newPosDwn
dwnBoo = True
elif newPosDwn == False and tempLocale == 1 and not newPosDwn in newCatch:
dwnBoo = False
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch =[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch.append(each)
newCatch = []
checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, width, height, forBoo, bakBoo, upBoo, dwnBoo)