Solution to coin flip streaks from "Automate the boring stuff with Python"
We won't always get the same result , simply because there is a ton of different combinations for 10000 coin flips , our list will be different each time we run the code, but the percentage of streaks that we will find each time will be around the same. Even the mathematical solution finds an approximate ( won't post it here, too complex and I doubt it will help you)
from random import randint streak = 0 expList = [] numberOfStreaks = 0 for experimentNumber in range(10000): # Code that creates a list of 10000 'heads' or 'tails' values. if randint(0,1) == 0: coin_flip = 'H' else: coin_flip = 'T' expList.append(coin_flip) for i in range(len(expList)): if i==0: pass # we don't do nothing when i=0 , otherwise we would get an error elif expList[i] == expList[i-1]: # checks if current list item is the same as before streak += 1 # best way I found so we count 7 Head or Tails in row else: # as 1 steak and not 2 streak = 0 if streak == 6: #checks if there is a streak of 6 heads or tails in a row. numberOfStreaks += 1 print (numberOfStreaks) print('Chance of streak: %s%%' % ((numberOfStreaks / 10000)*100)) # priting and finding percentage of the coin flips that # contain a streak of six heads or tails in a row #probability percentage = ( (no. of successful results) / (no. of all possible results) ) * 100%