title: Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object
body: <p>I have another problem with my code.
  I'm writing my first program in Vpython and I have to make a simulation of mixing two gases. First, I had a problem with borders, but now when the balls(that represents the gas particles) stay within the borders there is sth different wrong. After a few seconds, I get an error, which is shown below the source code of my function.</p>
  <p>Code:</p>
  <pre><code>def MovingTheBall(listOfBalls,position,numCell,flagOfExecution):
      flag = 0
      if flagOfExecution==0:
          positionTmp = position
      else:
          positionTmp = (position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0)
      for i in range( 0, len(listOfBalls) ):
          if positionTmp==listOfBalls[i].pos:
              flag=1
          
              
      if flag==1:
          return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
      else:
          if positionTmp[0]==0 or positionTmp[0]&gt;=numCell or positionTmp[0]&lt;=-numCell or positionTmp[1]&gt;=numCell or positionTmp[1]&lt;=-numCell:
              return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
  
          return positionTmp
  </code></pre>
  <p>the error is:</p>
  <pre><code>    return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 138, in MovingTheBall
      return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 138, in MovingTheBall
      return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 138, in MovingTheBall
      return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 138, in MovingTheBall
      return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 138, in MovingTheBall
      return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
    File &quot;gaz.txt&quot;, line 130, in MovingTheBall
      if positionTmp==listOfBalls[i].pos:
  RuntimeError: maximum recursion depth exceeded while calling a Python object
  </code></pre>
  <p>Can anybody think of a way to simplify my function?</p>
  <p>I run the function it while loop:</p>
  <pre><code>while 1:
          rate(20)
          for i in range(0,len(self.listOfBalls)):
              self.listOfBalls[i].pos=poruszanie(self.listOfBalls,self.listOfBalls[i].pos,self.numCell,0)
  </code></pre>