11.count -- I can't figure out where i am wrong


#1



11.count


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


i have tested my codes, it returns 0 with given aruments, have no idea why it says it returns 1


def count(sequence, item):
    s = sequence
    result = 0
    while item in s:
        result += 1
        s.remove(item)
    return result


#2

There might have been a 5 in that list before it was given to your function. There's no need to remove from the input (no benefit, just takes time), but if you absolutely want to use your current method then make a copy first. (assignment does not copy)

>>> a = []
>>> b = a
>>> b.append(1)
>>> a
[1]

#3

here is my code that works until you actively try to break it:

def count(sequence, item):
    z=0
    for x in sequence:
        if str(item) == str(x):
            z+=1
    return z

instead of doing that complicated removing business, you just pummel through each word of the sentence individually and leave them for potential manipulation later


#4

I have the exact same code as you but it doesn't work... why?
Oops, try again. Your function fails on count([6, 2, 3, 4, 5, 6],6). It returns 1 when it should return 2.`
Nvm found it, I put the increment thing += as =+


#5

:computer:


#6

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