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()
有帮助吗?

解决方案

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)

其他提示

The recursive line:

falseChecker(binList, r, c, size)

needs to be

return falseChecker(binList, r, c, size)

or the recursed function ends, and the outer function keeps going since it hasn't returned yet. It then finishes without returning, and so returns None.

You need a return at the end of falseChecker:

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

        #################################
        return falseChecker(binList, r, c, size)
        #################################

In Python, functions return None by default if they come to the end of themselves without returning. Furthermore, when falseChecker is run for the first time, if sCheck is False, then it will execute the else block. This code block doesn't contain a return. Because of this, the ultimate return value of falseChecker will be None.

I have the same cuestion, taking some exercices beyond my duties =), I've modified some codes to learn how to use decorators:

def listPrime(f):    #list the prime numbers
    def wrapper(x):
        for n in range(x):
            if f(n):
                print(n, end = " ", flush=True)
        print()

@listPrime
def isprime(n = 0): #evaluate when a number is prime (true) or isn't
    if n<=1:
        return False
    for x in range(2,n):
        if n % x == 0:
            return False
    else:
        return True

isprime(100)  

but in this case the console throw me this error "TypeError: 'NoneType' object is not callable", printing the type of the object (function) isprime() in this situation it says that it is actually a <class 'NoneType'>, but when I erase the @listPrime decorator, this status changes to a <class 'function'>. Why is that?

I Know there is another form to run the code:

def isprime(n):
    if n<=1:
        return False
    for x in range(2,n):
        if n % x == 0:
            return False
    else:
        return True

def listPrime(x):    #lista numeros primos
    for n in range(x):
        if isprime(n):
            print(n, end = " ", flush=True)
    print()

listPrime(200)

But I tried to modify it for the purpose of learning how to use decorators.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top