Count - Practice Makes Perfect


#1

Please, help! I am trying to get my head around the string methods, but it feels like I am missing out on something.

I tought this is one way to solve the "Count"-exercise:

def count(sequence,item):
    number=[]
    for i in sequence:
        if i==item:
            number +=1
        else:
            number = number
    answer=int(number)
    return answer

count

But it did not :blush:
Oops, try again. Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a "'int' object is not iterable" error.

I can better ask for some help... I thought that I made a function with two arguments, Γ‘nd that I turned the string into an integer.
Can anybody help me finding out what section of the course I did not understand?

Thank you for your help!!


#2

lets add a function call so we can see interpreter error:

def count(sequence,item):
    number=[]
    for i in sequence:
        if i==item:
            number +=1
        else:
            number = number
    answer=int(number)
    return answer
print count([1,2,3], 1)

number is a list, then you try to get the sum of list and 1, how does that work?

Why not make number an actual number (integer) which you can increase by one each time a match is found


#3

:blush: Thank you, Stetim94! I guess I should have thought about that...

Anyway, your answer was very helpfull. Thanks again!


#4

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