Why doesn't this work - Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a "an integer is required" error


#1



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


Oops, try again. Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a "an integer is required" error.


I know my code uses a different approach to the hint in this exercise, but I was wondering if someone could tell me why this doesn't work?


def count(sequence, item):
    new_seq = [sequence.pop(item)]
    times = len(sequence) - len(new_seq)
    return int(times)


#2

your function should also work for strings, and pop works based on index:

def count(sequence, item):
    new_seq = [sequence.pop(item)]
    times = len(sequence) - len(new_seq)
    return int(times)
print count([1,2,5],5)

so that can cause problems, the highest index is 2, so this is going to throw an error

if you want to do this, you would also need to use .index(), which finds the first index (even if there are multiply matches, so not sure if you can get it to work)


#3

Thanks! I must have been thinking of .index - didn't realised it only worked on the first match. Thanks!


#4