Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+3 votes

I have a random number of objects moving in my window. Any object that hits the border of any four sides of the window, it disappears(undraw). For this, I created a list and set the loop while len(list) != 0. When the object is undrawn, I remove it from the list. But, I got an error saying object not in list. I think its something to do with removing the elements from the list.

asked in CSC201 Spring 2021 by (1.1k points)
+1

It's hard to guess. You'll probably want to post some code here if you want more help figuring out the issue...

+1

while len(pizzaList)!= 0 and len(iphoneList)!= 0 and len(ballList)!= 0 and len(earthList)!= 0:

    for pizza in pizzaList:
        pizza.move()
        time.sleep(.1)
        if pizza.getCenterX() > WINDOW_WIDTH:
            pizza.undraw()
            pizzaList.remove(pizza)
            time.sleep(.1)
        if pizza.getCenterX() < 0:
            pizza.undraw()
            pizzaList.remove(pizza)
            time.sleep(.1)
        if pizza.getCenterY() < 0:
            pizza.undraw()
            pizzaList.remove(pizza)
            time.sleep(.1)
        if pizza.getCenterY() > WINDOW_HEIGHT:
            pizza.undraw()
            pizzaList.remove(pizza)
            time.sleep(.1)
    
    for iphone in iphoneList:
        iphone.move()
        time.sleep(.1)
        if iphone.getCenterX() > WINDOW_WIDTH:
            iphone.undraw()
            iphoneList.remove(iphone)
            time.sleep(.1)
        if iphone.getCenterX() < 0:
            iphone.undraw()
            iphoneList.remove(iphone)
            time.sleep(.1)
        if iphone.getCenterY() < 0:
            iphone.undraw()
            iphoneList.remove(iphone)
            time.sleep(.1)
        if iphone.getCenterY() > WINDOW_HEIGHT:
            iphone.undraw()
            iphoneList.remove(iphone)
            time.sleep(.1)
    
    for earth in earthList:
        earth.move()
        time.sleep(.1)
        if earth.getCenterX() > WINDOW_WIDTH:
            earth.undraw()
            earthList.remove(earth)
            time.sleep(.1)
        if earth.getCenterX() < 0:
            earth.undraw()
            earthList.remove(earth)
            time.sleep(.1)
        if earth.getCenterY() < 0:
            earth.undraw()
            earthList.remove(earth)
            time.sleep(.1)
        if earth.getCenterY() > WINDOW_HEIGHT:
            earth.undraw()
            earthList.remove(earth)
            time.sleep(.1)
    
    for ball in ballList:
        ball.move()
        time.sleep(.1)
        if ball.getCenterX() > WINDOW_WIDTH:
            ball.undraw()
            ballList.remove(ball)
            time.sleep(.1)
        if ball.getCenterX() < 0:
            ball.undraw()
            ballList.remove(ball)
            time.sleep(.1)
        if ball.getCenterY() < 0:
            ball.undraw()
            ballList.remove(ball)
            time.sleep(.1)
        if ball.getCenterY() > WINDOW_HEIGHT:
            ball.undraw()
            ballList.remove(ball)
            time.sleep(.1)

3 Answers

+3 votes

Im just guessing here but are you removing things from list by working backwards? Maybe that will solve error :)

answered by (1 point)
+1

whichever object reaches any four sides of the screen gets undrawn and removed from the list. so removing things from the list is kinda random.

+2 votes

Well, one possibility is that some pizza or ball or iPhone or earth has coordinates that are off the screen in TWO directions (e.g. x < 0 and y < 0), so it runs both your codes, and tries to remove the object twice.

To fix this, your IF/IF/IF/IF statements should be IF/ELIF/ELIF/ELIF.

What Janine said about working backwards when removing from a is also correct, although it probably isn't causing he error that you're seeing here. (If two consecutive objects in the list are both off the screen, then this bug will cause only the first one to get removed. However, the second one will presumably get removed the next time it moves, so it still won't get very far off the screen...)

answered by (508 points)
+2 votes

I would guess just that it might be indentation =or the item are being remove right before it got included?

answered by (1 point)
...