Count!


#1

I get This error->

Oops, try again. Your code looks a bit off--it threw a "'list' object has no attribute 'split'" error. Check the Hint if you need help!

def count(sequence,item):
    cnt=0
    lst=sequence.split()
    for i in lst:
        if item in lst:
            cnt=cnt+1
    return cnt

#2

We use split to create a list from a string. sequence is a list object. No need to construct a working list since we're only counting the elements.

>>> def count(sequence,item):
    c = 0
    for i in sequence:
        c += 1 if i == item else 0
    return c

>>> print (count([1,2,3,9,6,5,4,9,7,8,9], 9))
3
>>>

#4

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