Q:

python: Type Error while iterating over list consisting of tuples with a for-loop

i'm doing this assignment for school where I have to build a "game". The point of the game is to shoot a rocket into space in a certain way that it will hit a target  on the other side of the screen. The rocket has to go around planets and such and has to take their gravitational force into account... However I dont know what I'm doing wrong here:
planet1 = (vector(position1_x,position1_y), mass1)
planet2=...
planets = [planet1,planet2,planet3]
position_rocket = vector(positionrocket_x, positionrocket_y)

def gravforce(m,a,b):
    #a = position_planet, b = position_rocket, m = mass planet
   r = b-a
   return (-G * m/mag2(r)))) * norm(r)
   #this is just a formula to calculate the gravitational force produced by one planet

def total_gravforce(planets,position_rocket):
     total_grav = vector(0,0,0)
     for elements in len(planets):
         total_grav = total_grav + gravforce(planets[elements][1],planets[elements][0],position_rocket)
    return total_grav

As a result I get this: 
for elements in len(planets):
TypeError: 'int' object is not iterable
Thanks

A:

But you're not iterating over a list of tuples, you're operating over len() which returns an integer. Don't do that.
You probably meant to use range, but don't do that either: you should always iterate over the list itself, which gives you the elements directly:
for elements in planets:
    total_grav = total_grav + gravforce(elements[1], elements[0], position_rocket)

