1

I am trying to figure out this problem on Codecademy, but I can not figure it out. Ok so I need to make this so it will return the number of times a word is said in a list.

Here is what it says to do:

Write a function that counts how many times the string "fizz" appears in a list.

1.Write a function called fizz_count that takes a list x as input.

2.Create a variable count to hold the ongoing count. Initialize it to zero.

3.for each item in x:, if that item is equal to the string "fizz" then increment the count variable.

4.After the loop, please return the count variable.

For example, fizz_count(["fizz","cat","fizz"]) should return 2.

Then here is what I have written:

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

To look at this lesson the number is A Day at the Supermarket 4/13

1
  • Just for completeness: In Python you have the method .count() on lists, so your function could return just x.count('fizz'). Also, instead of count = count + 1 you can use count += 1. This is shorted and avoids doubling the code for the lvalue (count) in the idiom. Commented Mar 30, 2014 at 0:39

9 Answers 9

8

You're so close! You've just got the indentation wrong.

What you have:

for item in x:
    if item == 'fizz':
        count = count + 1
        return count # Returns any time we hit "fizz"!

What you need:

for item in x:
    if item == 'fizz':
        count += 1  # how we normally write this
return count   # after the loop, at the base indent level of the function
Sign up to request clarification or add additional context in comments.

Comments

4

You're returning count inside your for loop; you should finish iterating over your list, and then return.

Comments

4

You're almost there. The indentation is wrong. Your code should be:

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

2 Comments

TY IT WORKED!! :D Could I also do this with like "Count += 1"?
Yes, this will also work. Without the capital though: count += 1
3

What happens is that when a return statement is reached, the function will be exited. What you should do instead is to return the count after the for loop ends:

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

Note:

  • Indentation is extremely important in Python since that's the way blocks are grouped.

Comments

2
def fizz_count(words_list):
    return len([word for word in words_list if word == 'fizz'])

OR

def fizz_count(words_list):
    return words_list.count('fizz')

1 Comment

I know it doesn't answer your question, really, but give it a thought when you advance your Python (which you can start now).
2

You need to place the return statement after the for loop

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

2 Comments

Now you've got it. :)
Yeah, I'm not used to this syntax editor and I failed the indentation when I submitted the solution at first :P
2

put your return at the end of your for loop. so it would be:

    for item in x:
        if item == "fizz":
            count+= 1
    return count

3 Comments

Hint: Use leading four-space indentation to format code blocks.
@Two-BitAlchemist thanks I'm new and I wasn't sure, i got it now though!
NP. Markdown is really fun and useful (it's used here, git, and other places). There's also a wonderful utility called pandoc that can do all kinds of things with it. I encourage you to learn it!
1

return is part of the if block. Do not return until the for block has finished:

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

Comments

1

I'm aware that the assignment requires you to write an explicit for loop for counting, but this kind of error due to misindentation is exactly why I personally prefer using functional idioms in python. You can implement this like:

def fizz_count(x):
    return len([item for item in x if item == "fizz"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.