11. count


#1

Hello everyone im stuck at this code , it prints out correct which is 3 , but it still gives me an error stating that "Oops, try again. Your code looks a bit off--it threw a "invalid literal for int() with base 10: 'one'" error. Check the Hint if you need help!"

Here is my code:
def count(sequence,item):
item = int(item)
counter = 0
for x in sequence:
if x == item:
counter += 1
return counter

print count([1,2,1,1], 1)


#2

no worries i figured it out :slight_smile:

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

print count([1,2,1,1], 1)

#3

Well done mr_bashir, I have been trying to write a very similar code to yours but for unknown reasons it did not work, although I had a deep believing that it should work. so, I looked at the forum and I got this: "which I think is a bit advanced"

def count(sequence, item):
    found = 0
    for i in range(len(sequence)):
        if item == sequence[i]:
            found += 1
    return found
print count([4, 'foo', 5, 'foo'],'foo')

#4

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