Computer Science Canada

"*** can't assign to operator" error

Author:  OREO Speedwagon [ Tue Apr 05, 2011 1:08 pm ]
Post subject:  "*** can't assign to operator" error

I'm trying to make a simple version of Robot Finds Kitten as a Pygame project, and currently I'm trying to just draw a diagonal line of "box" items.

Python:

class Robot(pygame.sprite.Sprite):
    def __init__(self, image, position):
        pygame.sprite.Sprite.__init__(self)
        self.src_image = pygame.image.load(image)
        self.position = position
        self.xinc = self.yinc = 0
    def update(self, deltat):
        x, y = self.position
        x += self.xinc
        y += self.yinc
        self.position = (x,y)
        self.image = self.src_image
        self.rect = self.image.get_rect()
        self.rect.center = self.position


class Box(pygame.sprite.Sprite):
    def __init__(self, image, position):
        pygame.sprite.Sprite.__init__(self)
        self.src_image = pygame.image.load(image)
        self.position = position
    def update(self, newimage):
        x, y = self.position
        self.position = (x,y)
        self.image = self.src_image
        self.rect = self.image.get_rect()
        self.rect.center = self.position
        if pygame.sprite.collide_rect(robot, self):
            self.src_image = pygame.image.load(newimage)



rect = screen.get_rect()

robot = Robot("Robot.GIF", (0,0))
for i in range(1, 11):
    box + i = Box("closed.GIF", (10*i, 10*i))


is the relevant part of my code. The error is occurring in the final line of the sample. It's a popup, which I've never gotten before (although to use pygame we're using 2.5 instead of 3... maybe popups are normal in this version?) and it says "*** can't assign to operator". I'm not changing i, though... just generating a location for my box.

Author:  apython1992 [ Tue Apr 05, 2011 1:11 pm ]
Post subject:  RE:"*** can\'t assign to operator" error

This is because you are trying to assign a value to an expression. I'm guessing you want a list of boxes, which means you would use

code:
box[i] = Box()


: