Count


#1



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


could anyone tell me what is wrong with my code?

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


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

print count([1,2,"dust",3,3,3], 3)


#2

your code is fine. Seems you hit a glitch. Try refreshing the page/different browser


#3

Seconded. Your code is fine, seems to be a glitch in the matrix. Try a different browser. YOu can verify your code by running it in terminal, but I've also tried yoru code and it works just fine.


#5

well actually the code works fine, but there is a symantic error:

print count([1,2,"dust",3,3,3], 3)

and

print count([1,2,"dust",3,3,3], "3")

have same results, but they shouldnt.
the second one should result 0 but it results 3 because of str-conversion


#6

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