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?
为CS1开发一个Python项目,我遇到了一个奇怪的问题,我和我的室友都无法弄明白。代码的一般范围是使用数字来填充具有特定大小的形状的0的网格以填充空间,并且我们必须检查沿途以确保我们不将形状放置在已存在形状的位置。我在这里有两个函数,两者都做了几乎相同的事情,但无论出于什么原因,当falsechecker返回列表时它将它返回为NoneType。为什么会这样?
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()
3 个解决方案
#1
11
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
.
在sCheck == True为假的情况下,您不返回任何内容。在Python中,没有显式返回任何内容的函数返回None。
If you were trying to recursively call yourself and return the result, you wanted this:
如果你试图递归调用自己并返回结果,你想要这样:
return falseChecker(binList, r, c, size)
#2
3
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
.
或者递归函数结束,外部函数继续运行,因为它还没有返回。它然后完成而不返回,因此返回None。
#3
2
You need a return
at the end of falseChecker
:
你需要在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
.
在Python中,函数默认返回None,如果它们在没有返回的情况下到达它们自己的末尾。此外,当第一次运行falseChecker时,如果sCheck为False,则它将执行else块。此代码块不包含返回值。因此,falseChecker的最终返回值将为None。
#1
11
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
.
在sCheck == True为假的情况下,您不返回任何内容。在Python中,没有显式返回任何内容的函数返回None。
If you were trying to recursively call yourself and return the result, you wanted this:
如果你试图递归调用自己并返回结果,你想要这样:
return falseChecker(binList, r, c, size)
#2
3
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
.
或者递归函数结束,外部函数继续运行,因为它还没有返回。它然后完成而不返回,因此返回None。
#3
2
You need a return
at the end of falseChecker
:
你需要在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
.
在Python中,函数默认返回None,如果它们在没有返回的情况下到达它们自己的末尾。此外,当第一次运行falseChecker时,如果sCheck为False,则它将执行else块。此代码块不包含返回值。因此,falseChecker的最终返回值将为None。