11. Count



Your function fails on count([6, 2, 3, 4, 5, 6],6). It returns True when it should return 2.
Im sorry I'm having a rough day and cannot figure out what i am doing wrong any suggestions?

def count(sequence,item):
    return item in sequence


This will only return a boolean, True or False.

We need to iterate the sequence and keep a count of the instances of item in the sequence.

count = 0
for n in sequence:
    count += 1 if n == item else 0    # Pythonic ternary
return count


i see thank you! this helped a lot i see what i wasn't doing


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.