Count function


Hi Codecademy,

I am working through the python exercises, and am stuck on the Count assignment:

Define a function called count that has two arguments called sequence and item .
Return the number of times the item occurs in the list.

I came up with this code (which is quite minimal), and I am a little stuck. I think my code is quite simple and should be doing the job. However, for

count([1, 2, 1, 1], 1)
returns 4

count([“Hi”, “I am”, “Hi”, “Hi”, “I am”], “Hi”)
returns 5

count([“Hi”, “I am”, “Hi”, “Hi”, “I am”], “I am”)
returns 5

Could you please give me a “nudge” in the right direction?




lets study the values of item variable:

for item in sequence:
   print item

so you will just increase the counter for every item in the list. You will need to do some condition checking


I did it! Passed! Thanks for you relp.

Here is the code:

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