11. count


I already tried to figure out by myself but can't understand this error


Stick to 4 spaces for indentation.


I already tried this.
The problem in my code is the "return" testament.
But i don't know where is the problem


The problem in your screenshot is that you're using 4 spaces in some places and 3 in others for your indentation.

Use 4.


oh my...
Thank you for your help.!


I don't understand why my code is worng.
plz give me some advice.


You need use a range function in your loop


wow,, u r right, thx,0.0


Sorry, guys, I don't unterstand why my code doesn't work correctly. Help me, please!


Because of the if/else condition. You should delete the else statement and return "found" variable outside the for loop.


I've tried replacing for with if / while and none work. I don't see what is wrong.

def count(sequence,item):
....for item in sequence:
....return n-len(sequence)


What is wrong here? Assume the indentation is correct.

def count(sequence,item):
n = len(sequence)
print n
for i in range(0,n):
if sequence[i] == item:
sequence = sequence.remove(sequence[i])
return n-len(sequence)


It works!

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


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

This will work


Ok but how to make code working with numbers in string like input count(("1,2,3,4,5,61,1,"), 1)???


Hi All,

This is long way.

def count(sequence, item):
    n = 0
    if type(sequence) is str:
        ss = sequence.split()
        for i in ss:
            if i == item:
                n += 1
    elif type(sequence) is list:
        if sequence == item:
            n += 1
            sl = len(sequence)
            for x in range(sl):
                if sequence[x] == item:
                    n += 1
        for i in sequence:
            if i == item:
                n += 1
    return n

print count([4, 'foo', 5, 'foo'], 'foo')
print count([4, 'foo', 5, 'foo'], 5)
print count(["I", "'", "ve", "done", "it", "!!"] , "'")
print count([1, 2, 5, 4, 7], [1, 2, 5, 4, 7])
print count([4, 'foo', 5, 'foo'], [4, 'foo', 5, 'foo'])
print count("I think it'll in any cases","it'll")


If your still exploring various ways which work, I'll throw in my tuppence worth.

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