-1

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:

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)

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:

RecursionError: maximum recursion depth exceeded while calling a Python object.
perseus
  • 15
  • 1
  • 7
  • your not returning any value from the function `foo`, default to `None`. – lycuid Oct 11 '16 at 08:01
  • Please post the actual code, if it is minimal. If it is not, create a minimal version. Also post the full traceback, or in case of recursion error, the relevant parts of it. You also will not be able to access `some_output` outside of the function if you do not return it (+ a few other cases), at least not without a debugger... – Ilja Everilä Oct 11 '16 at 08:01
  • You have to include [MCVE] - that's less than _actual code_ but more than what you've included. – Łukasz Rogalski Oct 11 '16 at 08:02
  • 1
    return `x` instead of just `return` – ellaRT Oct 11 '16 at 08:08
  • I posted the actual code as requested. @IljaEverilä – perseus Oct 11 '16 at 08:12
  • Cannot repro your recursion error (this is why you should include the traceback as well, it is probably generated elsewhere). In order to store the results of `flip_coin` you could for example average over a list generated using a list comprehension (instead of a loop): `v_mins = [flip_coin(coins) for i in range(n)]`, since you do actually `return v_min`. If you're using python 3, use [`statistics.mean`](https://docs.python.org/3/library/statistics.html#statistics.mean) for averaging. – Ilja Everilä Oct 11 '16 at 08:16
  • Thanks, that actually works. So why do you think my for loop does not work outside the function even though I return v_min? @IljaEverilä – perseus Oct 11 '16 at 08:23
  • See the provided answers, you just weren't storing the return values of the function anywhere. As to the recursion error, could it be that your original code had the indentation as such that the for-loop calling `flip_coin` was actually part of the function body of `flip_coin`? – Ilja Everilä Oct 11 '16 at 08:25

2 Answers2

1

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

for i in range(n):
  v_min2 = flip_coin(coins)
  print v_min2

If you want average of everytime you call the function, you can simply do:

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

print (sum(average_list)/len(average_list))
lycuid
  • 2,555
  • 1
  • 18
  • 28
0

You are not assigning function call result. First collect all function calls so we can calculate average on them

v_min_list = [flip_coin(coins) for i in range(n)]

then we can find average with this answer Finding the average of a list

averag_v_min = sum(v_min_list) / float(len(v_min_list))
Community
  • 1
  • 1
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63