import random
numStreaks = 0
for experimentNum in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
outcome = []
for i in range(100):
if random.randint(0,1) == 0:
outcome.append('H')
else:
outcome.append('T')
# Code that checks if there is a streak of 6 heads or tails in a row
Hstreak = 0
for i in range(len(outcome)):
for x in range(6):
if outcome[x] == 'H':
Hstreak += 1
if Hstreak == 6:
numStreaks += 1
else:
numStreaks = 0
print ('Chance of streak: %s%%' % (numStreaks/100))
I'm having trouble with the coin flip project from chapter 4. I have no idea on what I'm missing or doing wrong. I tried to run the code, but nothing comes up. I've looked up other people's takes on the project, but I still have no clue why my code doesn't work.
There is a bunch of errors here. First of all, in this piece of code:
You are only ever checking first 6 flips from each set of 100. Only if they all are 'H' will your
numStreaks
get increased. Then, as @Emre Sahin said, you are setting yournumStreaks
to 0 every timeHstreak
is not 6. Lastly you are not resetting your Hstreak counter at any point even if streak is broken by T. All in all, you need to rethink your logic that is behind finding the streaks.