vendredi 22 avril 2022

Counting how many times value meets 2 times in a row in list

I need to count how many times 'Eagle' appears 2 times in a row in random generated list. In case ['Eagle', 'Eagle', 'Eagle', 'Eagle'] it should count 2, not 3

import random
def HeadsOrTails(amo):
amo1 = []
Coin = ['Eagle', 'Tails']
for i in range(amo):
    amo1.append(random.choice(Coin))
return amo1

count = 0

for i in range(len(HeadsOrTails(30))):
    if HeadsOrTails(30)[i] == 'Eagle':
        if HeadsOrTails(30)[i] == HeadsOrTails(30)[i+1]:
            count += 1
    else:
        continue

print(HeadsOrTails(30))
print(f' Eagle repeats {count} times in the list')

For some reason it calculates amount of values wrongly




Aucun commentaire:

Enregistrer un commentaire