11. Count



Oops, try again. Your function fails on count([4, 'foo', 5, 'foo'],5). It returns None when it should return 1.
I'm a tad bit confused... what's wrong with my code? I have a sneaking suspicion that I iterated through sequence incorrectly. However, If I did, I don't know how to fix it.

 def count(sequence, time):
    found = 0
    if time in sequence:
        for time in sequence:
            found += 1
        return found


found is only returned if time not in sequence

Your loop will find all items in in your list, not only time

On second thought maybe not. Here is what i would do:

loop over sequence:

for x in sequence:

then check if x equals time, if so increase counter

This design makes in my opinion more sense


Thank you. that totally worked. :relieved:

Also, facepalm for my misuse of else.


you might want to refine your loop as well, i can't see what you have now, but think the design of your loop through.


Yeah, here's what I ended up with:

def count(sequence, time):
    found = 0
    for x in sequence:
        if x == time:
            found += 1
    return found


very good :slight_smile: Well done :slight_smile:

Questions about it?


Nope. Thanks so much.