Count fonction


codecademy pop up :"Oops, try again. Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0.". the problem is that in the console i have the good return when i run my function with these parameters. but i can't continue because they don't have the same results ^^

def count(sequence, item):
    result = {}
    for it in item:
        ct = 0
        n = 0
        while n<len(sequence):
            if sequence[n]==it:
                ct +=1
    if len(result) == 1:
        return ct
        return result

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


I was trying different lists and your code works properly, but they are testing it with different scenarios in codecademy too and it seems one of them is giving this error ... but hard to figure it out. Let's see what others say.
btw, it is kind of weird code.


can you explain why it is a weird code for you plz. it is always interesting to view an another way of thinking.


as weird I meant that it is complex, so it could be simpler. My questions would be:
- why did you use a dictionary in result and why did you change item into list (3rd line)?
- do you need 2 loops (for and while), maybe only 1 will do the job?
- why do you have 2 return lines? will the code ever go to else: and if so what will it return?


Thanks for the explanation :

All the weird things you point is due to the fact that the item parmater can be a list with several number in it so i have to check for each number.
- That why result is a dictionnary to store all the results and return it
- That why i make a for loop to count for each number of the item list
- I transform the item in list so i can use my for loop even if ther is only one number to test
- The if else a the end is due to the fact that my function send back a dictionnary and the site want an integer so i cheat with him.

i may have misunderstood the sentence "The item you input may be an integer, string, float, or even another list!". So i can rewrote it considering that a list is not possible to be put in item parameter


i change my code without considering the item as a list and the website is happy so i can move on.

Here my new code more simpler for information :

def count(sequence, item):
        ct = 0
        n = 0
        while n<len(sequence):
            if sequence[n]==item:
                ct +=1
        return ct

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

thanks for the discussion.


Consider this as a simpler solution:

def count(sequence, item):
found = 0
for char in sequence:
if char == item:
found += 1
return found


I have made an equivalent function, but it still fails:

def count (sequence, item):
    total = 0
    for n in sequence:
        if n == item:
            total =+ 1
    return total

However, it throws:
"Oops, try again.
Your function fails on count([6, 2, 3, 4, 5, 6],6). It returns 1 when it should return 2."

Why in the world would it only count one of the 6s?


Herp, derp. Just saw the transposed = and + on the incrementer.


This worked fine:

def count(sequence,item):
    a = item
    for item in sequence:
        return sequence.count(a)
print count([1,2,3,1,3,1,1,1],1)

Kinda simple right?:wink: