Skip to content
Advertisement

ValueError: is not in list

I trying to make spaceship game from using pygame. Just simple game what warship shoot bullet and collide enemies. But when I play my game, after 6 or 7 collisions I get below error.

JavaScript

creating bullet class

JavaScript

creating enemy class and defining collide def

JavaScript

calling collide definition in for enemy loop

JavaScript

Then take this error after 6 or 7 time collision

JavaScript

Advertisement

Answer

This is your problem:

JavaScript

You are iterating over the lists and deleting elements from the lists while you are still going through them. That is not something that you are allowed to do.

Try iterating through a copy of the lists and deleting from the originals. Something like this:

JavaScript

On a different note, it is not clear to me why you are doing this kind of operation:

JavaScript

instead of using remove() like this:

JavaScript
Advertisement