简体   繁体   中英

TypeError: 'NoneType' object is not iterable

I have a function "checkAllInOneDirection" which is a recursive loop. When I break out of the loop, the function returns 1 array and 3 booleans. The weirdest thing is that within the recursive function, as it returns, it showed all of which have values yet I'm still getting the "None Type" error:

Here's the print:

Before return : [[0, 0]] False False False False

But as it returns and before it exception out, the values of returned became

    After returned : [[0, 0]] True True True True

Although the returned values out of the recursive is changed and wrong, there's still some value, yet I'm exception out of a "None Type" error? Here's the error:

     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

Here's the function that calls the recursive "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

Here's the recursive function checkAllInOneDirection code.

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)

I think you want the last line to be:

return checkAllInOneDirection(...)

instead of just:

checkAllInOneDirection(...)

After you fall off the end of that statement, the function returns. As it has no explicit return statement, it returns None .

finalCatch, forBoo, bakBoo, upBoo, dwnBoo = checkAllInOneDirection(...)

expects something of the form a,b,c,d,e on the right side of the equals. This is a syntactical shortcut for a list , a tuple , or a range . If a function doesn't return, or returns something that can't be iterated over, that's the sort of error you'd expect.

Iterating means the following: suppose you have a class that acts as a container for values. Iterating over the class or over the values is to examine each value one-by-one. If you're examining a class that doesn't support that sort of behavior (eg integers), it'll throw an error because there's no way to handle the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM