How to count a streak in heads and tails game

Viewed 23

I am making a coin flip simulator where the user inputs how many flips they would like to simulate. After the results are printed out I would like it to print the longest streak of heads and the longest streak of tails.

def coin_toss():
turn = 1
Heads = 0
Tails = 0
index = 1
number_of_flips = int(input('How many flips? '))

for _ in range(number_of_flips):
    flip = randint(1,2)
    if flip == 1:
        print(f'Result {index}: Heads')
        Heads += 1
        index+=1
    if flip == 2:
        print(f'Result {index}: Tails')
        Tails +=1
        index +=1
print('\n')
print(f'Number of Heads: {Heads}')
print(f'Number of Tails: {Tails}')
print('\n')
replay = input('Would you like to play again? y/n ')

while turn == 1:
    coin_toss()
if replay == 'y':
    pass
else:
    turn == 0
2 Answers

You can keep track of the results and then compute the longest sequence of consecutive duplicates from the results.

results = []
for _ in range(number_of_flips):
    flip = randint(1,2)
    results.append(flip)
    if flip == 1:
        print(f'Result {index}: Heads')
        Heads += 1
        index+=1
    if flip == 2:
        print(f'Result {index}: Tails')
        Tails +=1
        index +=1
heads_streak = max([sum(1 for y in x) for y,x in itertools.groupby(seq) if y == 1])
tails_streak = max([sum(1 for y in x) for y,x in itertools.groupby(seq) if y == 2])

Use this if you need to use a raw algorgithm. The function longest_streak is getting the sequence of coinflips adn the type that shall be counted as paremeter. Then it iterates through the sequence and increases the max_len counter when the char is equal to the type. Resets the counter when reaching another type.

from random import randint

def longest_streak(lst, type='H'):
    # this function will elaborate the longest sequence
    max_len = 0
    current_len = 0
    for i in lst:
        if i == type:
            current_len += 1
            if current_len > max_len:
                max_len = current_len
        else:
            current_len = 0
    return max_len


def coin_toss():
    turn = 1
    Heads = 0
    Tails = 0
    index = 1
    string_of_results = ""
    number_of_flips = int(input('How many flips? '))

    for _ in range(number_of_flips):
        flip = randint(1, 2)
        if flip == 1:
            print(f'Result {index}: Heads')
            # adds the coinfip type to the sequence -> in this case H
            string_of_results += 'H'
            Heads += 1
            index += 1
        if flip == 2:
            print(f'Result {index}: Tails')
            string_of_results += 'T'
            # adds the coinfip type to the sequence -> in this case T
            Tails += 1
            index += 1

    print("Sequence:",string_of_results)
    print("Lenght of longest head sequence",longest_streak(string_of_results))
    print("Lenght of longest tail sequence",longest_streak(string_of_results, 'T'))
    print('\n')
    print(f'Number of Heads: {Heads}')
    print(f'Number of Tails: {Tails}')
    print('\n')


while True:

    coin_toss()
    replay = input('Would you like to play again? y/n ')

    if replay != 'y':
        break
Related