11. count


#1

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


#2

Stick to 4 spaces for indentation.


#3

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


#4

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

Use 4.


#5

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


#8

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


#9

You need use a range function in your loop


#10

wow,, u r right, thx,0.0


#11

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


#12

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


#13

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

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


#17

What is wrong here? Assume the indentation is correct.

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


#18

It works!

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

#19

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

This will work


#20

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


#21

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
        else:
            sl = len(sequence)
            for x in range(sl):
                if sequence[x] == item:
                    n += 1
    else:
        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")


#22

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

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


#23

:computer: