函数在Python中返回一个NoneType? [英] Function Returning a NoneType in Python?

查看:906
本文介绍了函数在Python中返回一个NoneType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为CS1开发一个Python项目,我遇到了一个奇怪的问题,即我和我的爱人都不知道。代码的一般范围是使用数字填充具有一定大小的形状的0的网格以填充空间,并且我们必须检查以确保我们不会将形状放置在已存在形状的地方。我在这里有两个函数,两者都做几乎相同的事情,但无论什么原因,当falsechecker返回列表时,它将它作为NoneType返回。为什么会发生这种情况?

  def falseChecker(binList,r,c,size):
sCheck = isSpaceFree(binList ,r,c,size)
如果sCheck == True:
代表范围内的x(c,c + size):
代表范围内的y(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 block in blockList:
check = isSpaceFree(binList,r,c,i)
if check == True:在范围(c,c + i)中x为
:范围(r,r + i)中的y为

binList [x] [y] = i
c + = 1
如果c> len(binList):
c = 0
r + = 1
if r> len(binList):
return binList
else:
binList = falseChecker(binList,r,c,i)

return binList

main()


解决方案

$ c> sCheck == True 为false,您不需要 return 任何东西。在Python中,一个函数不会显式 return 任何东西都会返回 None



如果您尝试以递归方式调用自己并返回结果,则需要这样做:

  return falseChecker (binList,r,c,size)


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)

这篇关于函数在Python中返回一个NoneType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆