11. Count



I get the following error: > Oops, try again. Your function fails on count([1],7). It returns 1 when it should return 0.

I'm looking to iterate the item through the sequence. And everytime there is a match, it should count up +1. For some reason I lost it. Have no idea how to get this thing to work. Thanks in advance!

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


You're not really counting the matches but rather counting everything in the sequence. item becomes an index of the loop and no longer the input variable, item. So you need to change your index to something other than item and validate whether the item in the sequence matches item using an if statement.

The loop goes through every item in your list so you're adding 1 to the total every time you pass an item in the list.


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