Count


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/1?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0.


The function returns 0 when tested.. not 1.


def count(sequence, item):
    found = 0
    while item in sequence:
        sequence.remove(item)
        found += 1
    return found
        
print count([4, 'foo', 'foo'], 5)


#2

Hi, @ronberko3 ,

This alters the original list ...

sequence.remove(item)

Instead of removing elements from the list, check for each element in the list whether it is equivalent to item. If it is, then increment count, without removing the element.


#3

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