Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法解压缩不可iterable非类型对象-返回元组_Python 3.x_Tuples_Iterable Unpacking - Fatal编程技术网

Python 3.x 无法解压缩不可iterable非类型对象-返回元组

Python 3.x 无法解压缩不可iterable非类型对象-返回元组,python-3.x,tuples,iterable-unpacking,Python 3.x,Tuples,Iterable Unpacking,我还不明白为什么这个python代码不起作用;这是我们正在做的一个小游戏的一部分。 以下函数是类的一部分: def detectCollision(self,other): if(self.x < other.x + other.w and self.x + self.w > other.x and self.y < other.y + other.h and self.y + self.h > other.y): retu

我还不明白为什么这个python代码不起作用;这是我们正在做的一个小游戏的一部分。 以下函数是类的一部分:

def detectCollision(self,other):
    if(self.x < other.x + other.w and
    self.x + self.w > other.x and
    self.y < other.y + other.h and
    self.y + self.h > other.y):
        return (self,True)
但我在运行代码时出错: “无法解压缩不可编辑的非类型对象。”

我从其他帖子中了解到,当试图仅用一个值填充元组或函数不返回任何值时,就会出现这种情况。。。但是我的函数返回一个元组,所以我不知道为什么它不能被解包。我还尝试删除元组周围的括号

有人能提出补救办法吗


谢谢

正如@m.i.cosacak在评论中所说,您只需以元组的形式添加另一个返回语句,以保持其返回值的一致性:

def detectCollision(self,other):
    if(self.x < other.x + other.w and
    self.x + self.w > other.x and
    self.y < other.y + other.h and
    self.y + self.h > other.y):
        return (self, True)

    return (self, False)
def检测碰撞(自身、其他):
if(self.x其他.x和
self.yother.y):
返回(self,True)
返回(self,False)

因此,如果条件为
false
,则仍将返回一个元组。

函数仅在条件为true时返回元组,如果条件为false,则将在函数末尾返回
None
add
else返回(self,false)
。啊,我明白了!是的,好的。非常感谢。
def detectCollision(self,other):
    if(self.x < other.x + other.w and
    self.x + self.w > other.x and
    self.y < other.y + other.h and
    self.y + self.h > other.y):
        return (self, True)

    return (self, False)