11. count


Why am I getting Oops, try again. Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a "list indices must be integers, not str" error.

def count(sequence, item):
    listi = []
    for x in sequence:
        if item == sequence[x]:
            listi += item
    return listi


Please provide a link whenever you post a question :slight_smile:

def count(sequence,item): 
    listi = [] 
    for i in sequence:
        if x == item:
    return len(listi)

Ah, so what you want to do is take item, not sequence, and append x to listi. You made listi equal to itself plus item.
The instructions say:
Return the number of times the item occurs in the list.
Since you append x to the empty list, and then say len(listi), that will return how many times the item appears in the list, since the list is the same item repeated multiple times.


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