I want to total up a variable that changes every time a function is run.
import random def flip(): h,t = 0,1 cheads, ctails, = 0,0 cflips = random.choice((0,1)) if cflips == 0: cheads += 1 return 'h' else: ctails += 1 return 't' def repeat(f,n): for i in range(n): flip() results = "" while not results.endswith("hth"): results += flip() else: flips = len(results) print "it took" , flips , "to find HTH" repeat(flip, 10000)
It's that flips variable towards the end. Every time the function executes, it changes. I want to run the flip() function 10,000 times and add up the flips variable 10,000 times as well. Usually, the number of flips is somewhere around 8-16, so my total should be somewhere around 80,000-160,000.