#11.Count: is there an incongruence?


#1



CHECH #11. COUNT


#2

THIS CODE PASSES SUCCESFULLY, BUT OUTPUT 0 IS WRONG

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

print count("one two one thre one", "one")

THIS CODE DOESN'T PASS, BUT RUNS SMOOTHLY ON LICLIPSE AND OUTPUT 3 IS CORRECT

def count(sequence, item):
    found = 0
  
# HERE I SPLIT THE SEQUENCE 
    for i in sequence.split():
        if i == item: 
            found += 1
    return found

print count("one two one thre one", "one")

WHY ?


#3

well, the exercise will test lists on your function, which works in your first example:

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

print count(["one", "two", "one", "thre", "one"], "one")

but not in your second. The reason the second code is working in eclipse, is because string does support split, but lists don't (which is what the exercise throws at your function)


#4

oooops !

I was getting if fast and skipped the detail…

Thank You stetim94 !

I was NOT checking a list within:

print count("one two one thre one", "one")

That's obvious, now.