Lists + Functions


#1

While doing the lesson, i wrote this and got a correct answer. Now, i get that the function did its purpose, since it did not found any fizz, it returned zero. I cant seem to grasp where i should put the list that should be checked for string fizz ?

# Write your function below!
def fizz_count(x):
    count = 0
    for n in x:
        if n == "fizz":
            count = count + 1
    return count

#2

Hello, @hexapod666!
Edit: Changed my answer, got what you mean.

The list is supposed to be passed outside and after the function has been created.
Simply do the following:

myList = ["fizz","cat","fizz"] #You could also pass only the list, there's not really a need to store it into a variable
fizz_count(myList)

Quote me if there's anything else I can help you with!

Best regards,
g4be


#3

Hello @g4be

I cant get the correct output for my code

Write your function below!

def fizz_count(x):
    count=0
    for item in x:
        if(item=='fizz'):
            count+=count
    return count
myList = ['fizz','buzz']
z=fizz_count(myList)
print z

The output is 0 and I get an error saying that it returned zero instead of the correct answer


#4

That's your problem!
You're supposed to increase the value in the variable count because this way you're only adding zero with zero, over and over again.

Best regards,
G4be


#5

R8!! Thanks.. Completely missed it :slightly_smiling:


#6

No problem! Glad to help!