'Heads or Tails, I am unable to get the average of the flips, i have tried putting the number of flips in a list but no luck. Please assist

Create a python program that uses a random number generator to simulate flipping a coin several times. The simulated coin should be fair, meaning that the probability of ‘heads’ is equal to the probability of ‘tails’. Your program should flip simulated coins until either 3 consecutive ‘heads’ or 3 consecutive ‘tails’ occur. Display a H each time the outcome is ‘heads’, and a T each time the outcome is ‘tails’, with all of the outcomes shown on the same line. Then display the number of flips needed to reach 3 consecutive flips with the same outcome. When your program is run, it should perform the simulation 10 times and output the average number of flips needed. Use functions in your solution.

enter image description here

import random

for n in range(1,11):
    flips = [0,0,0]
    i = 0 
    H = 0 
    T = 0 
    x = 0
    y= 0  
    while True:
        r=random.randint(1,2)
        flips[i%3]=r
        i+=1
        if r==1:
            H +=1
            print(end = " H ") 
        else:
            T+=1
            print(end = " T ", )        
        if flips == [1,1,1] or flips ==[2,2,2]:
            print("(",i , "flips )")
            break

I need to display the average amount of flips it took to break the loop, if that makes sense. Every time I try to use a.appened(i), it gives me the last value of i, how would I go about adding every number of flips.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source