I am trying to build a program that will create a random list of Heads or Tails ('H', 'T') and then count how many times either the 'H' or the 'T' is repeated 6 times. I think I have figured out the random list, which works fine when i run it independently. But when it comes to the counting, I'm not sure how to make python detect a sequence of 6 identical letters in a list.
for x in range(0, len(coin_list)-1):
if coin_list[x] == coin_list[x+1] and coin_list[x] == coin_list[x+2] and \
coin_list[x] == coin_list[x+3] and coin_list[x] == coin_list[x+4] and \
coin_list[x] == coin_list[x+5]:
streak_counter+=1
else:
continue
if streak_counter ==6:
numberOfStreaks+=1
streak_counter=0
else:
continue
First this block checks each value in the list coin_list and also checks if all 5 values that come after it are identical to the first value. If they are, then the streak_counter variable is increased by one. Then the program checks if the streak_counter variable is equal to 6, if it is, then the numberOfStreaks variable is increased by one, and the streak_counter is reset to 0.
When I run this program I don't get my expected outcome. The program always returns that numberOfStreaks is equal to 0, and therefore the percentage of streaks is 0%.
Not sure where I'm going wrong here. Any help would be much appreciated.
import random
numberOfStreaks = 0
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
for i in range(100):
coin_list=[]
random_num=random.randint(0, 1)
if random_num == 1:
coin_list.append('H')
elif random_num == 0:
coin_list.append('T')
# Code that checks if there is a streak of 6 heads or tails in a row.
for x in range(0, len(coin_list)-1):
if coin_list[x] == coin_list[x+1] and coin_list[x] == coin_list[x+2] and \
coin_list[x] == coin_list[x+3] and coin_list[x] == coin_list[x+4] and \
coin_list[x] == coin_list[x+5]:
streak_counter+=1
else:
continue
if streak_counter ==6:
numberOfStreaks+=1
streak_counter=0
else:
continue
print(numberOfStreaks)
print('Chance of streak: %s%%' % (numberOfStreaks / 100))