How to average over the output values of a function
<p>I am relatively new to python so I apologize for any unconventionality in my code.
 I have a function which returns some values. I want to repeat this function n times  and get the average over the output values for the n iterations (possibly without defining a new function). How can this be done? For example:</p>

<pre><code>def flip_coin(coins):

    pr_head = 1
    pr_tail = -1
    options = (pr_head, pr_tail)
    simulation = [[random.choice(options) for x in range(10)] for y in range(coins)]

    rep = [i.count(1) for i in simulation]
    m = min(rep)
    ind_m = rep.index(m) 

    c_min = simulation[ind_m]
    v_min = c_min.count(1) / 10

    return v_min

for i in range(n):
    flip_coin(coins)
    #take average over(v_min)
</code></pre>

<p>However I cannot access 'v_min' outside of the function scope and when I iterate the function inside the function (with the exact for loop as above) I get the error:</p>

<pre><code>RecursionError: maximum recursion depth exceeded while calling a Python object.
</code></pre>

<p>Is that how you want it, you need to assign the return value to a variable to access it later:</p>

<pre><code>for i in range(n):
  v_min2 = flip_coin(coins)
  print v_min2
</code></pre>

<p>If you want average of everytime you call the function, you can simply do:</p>

<pre><code>average_list = []
for i in range(10):
  average_list.append((flip_coin(coins)))

print (sum(average_list)/len(average_list))
</code></pre>

python iteration average