I am struggling with the solution of the 'Coin Flip' practice project at the end of chapter 4 in 'Automate the boring stuff' for python programming.
I have two solutions, both yielding a totally different result (first one is clearly false). I am not sure, what is the right solution to the answer.
Solution 1:
import random nextFlip = [] numberOfStreaks = 0 # Code that creates a list of 10000 'heads' or 'tails' values. for expNum in range(10000): select = random.randint(0,1) if select == 0: nextFlip.append('H') elif select == 1: nextFlip.append('T') # Code that checks if there is a streak of 6 heads or tails in a row. for i in range(0,(len(nextFlip)-6)): if nextFlip[i] == nextFlip[i+1] == nextFlip[i+2] == nextFlip[i+3] == nextFlip[i+4] == nextFlip[i+5] != nextFlip[i+6]: numberOfStreaks +=1 print('Chance of streak: %s%%' % ((numberOfStreaks / 10000)*100)) Solution 2:
import random nextFlip = [] hlist = 0 tlist = 0 numberOfStreaks = 0 # Code that creates a list of 10000 'heads' or 'tails' values. for expNum in range(10000): select = random.randint(0,1) if select == 0: nextFlip.append('H') elif select == 1: nextFlip.append('T') # Code that checks if there is a streak of 6 heads or tails in a row. for i in range(0,(len(nextFlip)-6)): if nextFlip[i] == 'H': hlist += 1 if hlist == 6: numberOfStreaks +=1 elif nextFlip[i] == 'T': tlist += 1 if tlist == 6: numberOfStreaks +=1 print('Chance of streak: %s%%' % ((numberOfStreaks / 10000)*100)) Maybe someone can help me and tell me what I did wrong.
random.choice(('H', 'T'))instead ofrandom.randint(0, 1)and then converting the numbers to letters.forloop is indented one level too deep.forloop shouldn't be inside the firstforloop.