Reputation: 426
I don't yet understand why this python code doesn't work; it's part of a little game we are making. The following function is part of a class:
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)
Later I call this function from within a loop:
for coin in coins[:]:
(player,collided) = bob.detectCollision(coin)
if collided:
coins.remove(coin)
But I get an error when I run the code: "cannot unpack non-iterable NoneType object".
My understanding from other posts is that this occurs when trying to populate a tuple with only one value or the function doesn't return anything... but my function returns a tuple, so I am at a loss as to why it cannot be unpacked. I also tried removing the brackets around the tuple.
Can anyone please kindly suggest a remedy?
Thank you
Upvotes: 2
Views: 1512
Reputation: 687
As @m.i.cosacak said in the comments you simply need to add another return statement in the form of a tuple to keep its returned values consistent:
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)
so now if the condition is false
then a tuple will still be returned.
Upvotes: 1