title: How to use the last returned value by a function, as an input for the same function in a loop. Python
body: <p>I have a problem using the last returned value from a function, as an input for the same function, I don´t know if it is possible to do.</p>
  <p>For example, I have the following function:</p>
  <pre><code>def sample (x):
      p=1+x
      return p 
  sample(h)
  </code></pre>
  <p>and I want to use the last returned value &quot;p&quot; as the new data for h in a loop, the new function will look like:</p>
  <pre><code>def sample (x):
      p=1+x
      return p 
  sample(h)
  
  for i in range (0,5):  
      h=sample(h)
  </code></pre>
  <p>The code works for iteration 1 and 2, but don´t update the values for iterations 3,4,5. Variable &quot;p&quot; in  my real code, takes values from other functions or databases and changes (it´s a 3d array too), so it changes with each iteration. First input data &quot;h&quot; comes from a previous function too.</p>
  <p>The input and output will be like:</p>
  <pre><code>h= [[[1.71, 1.8,  1.32, 1.56, 2.81],   [1.,   2.,   1.,   2.,   1.]],
      [[1.44, 1.47, 1.5,  1.02, 2.51],   [1.,   2.,   1.,   2.,   1.]]]
  
  
  p= [[[1.62, 1.15, 1.1,  1.05, 2.28],   [1.,   2.,   1.,   2.,   1.]],
      [[1.97, 1.85, 1.88, 1.03, 1.87],   [1.,   2.,   1.,   2.,   2.]]]
  </code></pre>
  <p>I will be very thankful if you can help. BR.</p>