Did I do something wrong


def fizz_count(x):
    count = 0
    for item in x :
        if item == "fizz":
            count = count +1
        return count

fizz = ["buzz","fizz",7,"fizz","fizz",5]
answer =fizz_count(fizz)

print answer


a function ends the moment a return keyword is reached

So your function ends in the first iteration/run of your loop, but to successfully determine the number of fizzes in the list, the whole loop needs to finish running


Thank you again. I didn't realize I had an error with my indexing . In pushing the return statement back a little I received the answer that I wanted

white space can be a little bit of a pain sometimes


because now the whole loop can run, resulting in the correct amount of fizzes in list

yep, many other languages use curly brackets, which can also be tricky, especially because you might get lazy and don't indent the code, causing a mess

both methods (indent, curly brackets) have there pros and cons

even with indent, a } can be easy to miss


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