Count


#1



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


I am working with python 3, but even if I run it on python 2 in other platforms it returns 0. Just in this place would return 1.
I would really appreciate your comments


Replace this line with your code. 
def count(sequence, item):
    c=0
    while item in sequence:
        c+=1
        sequence.remove(item)
    return c


#2

The message from Codecademy is misleading. The content of the list, [4, 'foo', 'foo'], is what remains after your code has removed the item, 5, from it.

Instead of removing items from sequence as you count them, rewrite your code so that it counts the items without removing them. It may help to use a for loop instead of a while loop.


#3

Thank you. I already know how to make it work with the for loop, but I would like to know how to make it work with the while and remove()


#4

Your code does count correctly the occurrences of item that were originally in sequence. However, regardless of whether or not it is fair, Codecademy compares your result to the number of occurrences that remain after your function has removed all of them, leaving 0 occurrences. Therefore, your code fails any of Codecademy's tests that begin with 1 or more occurrences of item in sequence.


#5

Ohhh! I see, thank you


#6