11. Count


#1


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

Your function fails on count([6, 2, 3, 4, 5, 6],6). It returns True when it should return 2.
Im sorry I'm having a rough day and cannot figure out what i am doing wrong any suggestions?


def count(sequence,item):
    return item in sequence


#2

This will only return a boolean, True or False.

We need to iterate the sequence and keep a count of the instances of item in the sequence.

count = 0
for n in sequence:
    count += 1 if n == item else 0    # Pythonic ternary
return count

#3

i see thank you! this helped a lot i see what i wasn't doing


#4

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