Having a problem with a code for homework. Basically what I’m trying to do is take in a list of objects and pass them to my fire method.
def fire(self,targets):
i = 0
for i in targets:
x,y = targets[i].position
tx,ty = self.position
d = getDist(targets[i].position, self.position)
Whenever I call the fire method and pass in the objects it points me to line 17 which is the x,y = targets[i].position line and says “TypeError: list indices must be integers, not Bomber”
Bomber is the name of the class. I call the fire method like this:
bOne.fire([bTwo, tOne, tTwo, tThree])
Any help is much appreciated.
You are looping over the list itself, which means you don’t need to use the values as an index:
In python, the
forconstruct doesn’t work just with numbers, it works directly with the elements of the sequence you are looping over.