Function Returning a NoneType in Python?

BLU picture BLU · Oct 23, 2013 · Viewed 24.7k times · Source

Working on a Python project for CS1, and I have come accross a strange issue that neither I or my roomate can figure out. The general scope of the code is to fill in a grid of 0s with shapes of a certain size using numbers to fill the space, and we have to check along the way to make sure we arent putting shapes in places there there are already shapes. I have two functions here, both do virtually the same thing, but for whatever reason when falsechecker returns the list it returns it as a NoneType. Why is this happening?

def falseChecker(binList, r, c, size):
    sCheck = isSpaceFree(binList, r, c, size)
    if sCheck == True:
        for x in range(c, c+size):
            for y in range(r, r+size):
                binList[x][y] = size
        return binList
    else:
        c += 1
        if c > len(binList):
            c = 0
            r += 1
            if r > len(binList):
                return binList
        falseChecker(binList, r, c, size)





def iChecker(binList, blockList):
    r = 0
    c = 0
    for i in blockList:
        check = isSpaceFree(binList, r, c, i)
        if check == True:
            for x in range(c, c+i):
                for y in range(r, r+i):
                    binList[x][y] = i
            c += 1
            if c > len(binList):
                c = 0
                r += 1
                if r > len(binList):
                    return binList
        else:
            binList = falseChecker(binList, r, c, i)

    return binList

main()

Answer

abarnert picture abarnert · Oct 23, 2013

In the case where sCheck == True is false, you don't return anything. And in Python, a function that doesn't explicitly return anything returns None.

If you were trying to recursively call yourself and return the result, you wanted this:

return falseChecker(binList, r, c, size)