Count


#1

What is wrong with the code!
My code :

def count(squence, item):
    total = 0
    for x in squence:
        print x
        if x == item:
            total = total + 1
        return int(total)

Error :
Oops, try again. Your function fails on count([4, 'foo', 5, 'foo'],5). It returns 0 when it should return 1.


#2

By default, a function returns None at the end. If we want to return something else at the end of the function, we can use the return keyword

there is the catch, return is the last thing a function does. So, your function ends in the first iteration of the loop, but to get the correct total, the loop needs to make all its iterations


#4

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