11. Count


#1

I am stuck on the "count" exercise (Practice makes perfect). It throws the following error messeage:

Oops, try again. Your function fails on count([7.8, [], 7, 'buggy'],7). It returns 2 when it should return 1.

But shouldn't the function return 2? The way I see it there is a float (containing one 7), an empty list, an integer (being another 7) and a string. So in total two 7s.

Did I misunderstand the instructions?


def count(sequence, item):
    result = 0
    for i in sequence:
        if isinstance(i, int):          
            temp_result = str(i).count(str(item))
            result += temp_result
        elif isinstance(i, float):
            temp_result = str(i).count(str(item))
            result += temp_result
        elif isinstance(i, str):
            temp_result = i.count(str(item))
            result += temp_result
        elif isinstance(i, list):
            temp_result = i.count(item)
            result += temp_result
        else:
            pass
    return result


#2

We are not cataloging or counting items of each type, only those that match the item given in the arguments. 7.8 and 7 are different.


#4

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