11. count ( Doubt )


#1
def count(sequence, item):
    found = 0
    seq_list = []
    for i in sequence:
        if i == item:
            found += 1
    print found
    return found

count('I am I am am', 'am')

Though am able to move to the next lesson, but on console its printing 0 as count for 'am' while calling count('I am I am am','am'). But for 'I' it is printing 2 as count which is absolutely right


#2

The moment you loop over a string, it will go letter by letter, you can see this nicely if you add a simple print statement:

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

count('I am I am am', 'am')

you will see each letter being printed. One letter will never equal am, so there is your problem. i recommend to use lists for this


#3

Yeah, Thanks. I was just wondering that they are accepting the code in this case then something is wrong somewhere.


#5

You need help @bikbula? Posting full answers without any explanation is not allowed, please read the guidelines, thank you for your understanding


#6

what variable seq_list = [ ] doing there? you don't need that line.


#7

I have no idea, i just modified peeyushchawla code until it worked. I didn't notice the seq_list