count....What wrong with my code?


#1



output is equal to 0 but not accepted by website,Why?????
Oops, try again.
Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0.


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

print int(count([4, 'foo', 'foo'],5))


#2

i think the problem is that your function call:

print int(count([4, 'foo', 'foo'],5))

Does some weird integer casting, where the function call of the exercise does not. Try the function call without int(), to see what the output is


#3

okay, but the real problem is here:

new_list = sequence

this doesn't make a copy of the list. it just creates a variable which refers to the list, try copying the list (sequence) into new_list


#4

Excuse me, In this new, Is there any mistake???
We have answer but not accepted!
While I checked it in another IDE and our output was 0

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

print count([4, 'foo', 'foo'],5)

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


#5

like i said, you should make a copy of the list:

def count(sequence,item):
    found = 0
    new_list = list(sequence)
    while item in new_list:
        found += 1
        new_list.remove(item)
    return found 

print count([4, 'foo', 'foo'],5)

then it does work, let me try to find out why this is the case


#6

it seems you can use remove directly on the original list (sequence) because of the way your code is validated), which is why you need to make a copy (if you want to go with your solution)


#7

I got it, Thanks a lot my friend:bouquet: