# In this program, we flip 100 coins, 1000 times, and we # compute the proportion of these trials in which between # 40% and 60% land heads. Try changing the number of coins # and see how this proportion changes. How many coins do you # need so that in at least 95% of the trials, the proportion # is between 40% and 60%? import random def pctheads(n): v = [random.randint(0,1) for _ in range(n)] count = 0. for i in v: if i == 1: count += 1 return count/n def close(n,trials): v = [pctheads(n) for _ in range(trials)] count = 0. for i in v: if i <= .6 and i >= .4: count += 1 return count/trials print close(100,1000)