Count function with list


#1

My result appears to be correct according to code academy but while testing it, the function is returning the wrong result:

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

print count([“mary”, 4, 4, “jon”, “bam”, [“jason”, “mary”, 4, [4,4,“jason”]],4], 4)

This returns 3 when the answer should be more than 3.

Can anyone look into why the f’n appears to be correct according to codeacademy and/or why the result is wrong?

thx


Why is my count incorrect?
#2

the function is correct, but it can’t handle a list. If you do:

for i in sequence:
   print i

you will see that i is an entire list, which will never equal 4

so if you want to handle list, you will need to expand the function


#3

Ah I see, is there a simple way to deal with lists within lists where a user may input an argument that could be but is not necessarily a list within a list?


#4

yes, but don’t you want some time to think about this yourself? Do more you do yourself, the better it would be. Given i just gave you new insight (hopefully). And coming up with solutions for this kind of problem is such a valuable learning opportunity.

Of course, if you can’t figure it out, let me know, then i will give you a hint


#5

Fair. Will have a think/revert to this later and reach out if I get no where on it.


#6

True, but if you already want a hint, you can ask for it. I mean, its your learning process. I just expressed my opinion about learning opportunity.


#7

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