11. Count


#1

https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/1?curriculum_id=4f89dab3d788890003000096#

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
    else:
        return found

#2

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


#3

Thank you. that totally worked. :relieved:

Also, facepalm for my misuse of else.


#4

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.


#5

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

#6

very good :slight_smile: Well done :slight_smile:

Questions about it?


#7

Nope. Thanks so much.


#8