title: How to transfer a value from an innermost while loop to the outer for loop in python?
body: <p>Let's assume there is a big sphere with known center (Xo,Yo,Zo) and radius Ro. It contains millions of particles each with known mass and 3d position with respect to the same reference frame. Inside this big sphere there are a dozen of smaller imaginary spheres randomly distributed in the 3d space but otherwise known positions. I would like to calculate the number of the particles inside each smaller sphere, and hence, the resulting mass of each one of the smaller spheres (containers) by counting those particles that are inside each one of them.</p>
  
  <p>Here is my MWE snippet in which I am looping over individual particles to see if they are within each of the smaller sphere and then counting them individually for each sphere in order to come up with a total mass for each smaller sphere:</p>
  
  <pre><code>import numpy as np
  from scipy.spatial.distance import euclidean 
  
  ### there is 10 small spheres inside Big Sphere each containing different number of particles and hence different masses
  ### small_sphere_id_array is the array of the IDs of the small spheres
  ### small_sphere_position_array is the array of the position of the center of small spheres
  ### small_sphere_radius_array is the array of the radii of small spheres
  ### small_sphere_mass_array is the array of the masses of small spheres
  ### particle_position_array is the array of the positions of particles inside the Big Sphere
  ### particle_mass_array is the array of the masses of particles inside the Big Sphere
  
  for small_sphere_index in np.arange(0, 10)):
  
      for particle_index in np.arange(0, 6000000)):
  
          small_sphere_mass_array = []    
          small_sphere_mass = 0
  
          distances = euclidean(particle_position_array[particle_index], small_sphere_position_array[small_sphere_index])
          success_condition = (distances &lt;= small_shpere_radius_array[small_sphere_index])
  
          while success_condition:
              small_sphere_mass += particle_mass_array[particle_index]         
              small_sphere_mass_array.append(small_sphere_mass)
              small_sphere_mass = np.sum(small_sphere_mass_array)
  
          else:
              break
  
  
      print('{}, {}'.format(small_sphere_id_array[small_sphere_index], small_sphere_mass))
  </code></pre>
  
  <p>I am expecting to have printed out 10 lines (corresponding to 10 small spheres) with their IDs first followed by their total mass. But, here is the output I have:</p>
  
  <pre><code>some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  some number, 0
  </code></pre>
  
  <p>In particular I cannot take the result of <code>while</code> loop and move it to the outer <code>for</code> loop and hence loop over all particles to come up with a single nonzero total mass for each small sphere. I do seem to correctly loop over 10 small spheres but with the problem that the mass ends up being zero which means the middle <code>for</code> loop is not able to account for all 6000000 particles. (hint: all particles <em>do</em> have mass.) </p>