2

i am trying to use codeacademy to learn python. the assignment is to "Write a function called fizz_count that takes a list x as input and returns the count of the string “fizz” in that list."

# Write your function below!
 def fizz_count(input):
     x = [input]
     count = 0
     if x =="fizz":
         count = count + 1
         return count

i think the code above the if loop is fine since the error message ("Your function fails on fizz_count([u'fizz', 0, 0]); it returns None when it should return 1.") only appears when i add that code.

i also tried to make a new variable (new_count) and set that to count + 1 but that gives me the same error message

I would appreciate your assistance very much

6
  • input is already a list. You do not need to nest it inside another list literal. Commented Jul 16, 2013 at 1:27
  • By the sounds of it, you're just after return input.count('fizz') - you don't need to retain a counter around in your fizz_count function. At the moment your function is always going to fall off the end of the function and return None... Commented Jul 16, 2013 at 1:28
  • jon clements, that worked :D do you want to post that as an answer ? so i can upvote and set as solution ? Commented Jul 16, 2013 at 1:30
  • @Jon: Although input.count does the work for you, it would be more pedagogical to implement it oneself. @user1476390: your implementation needs a for loop, at the very least. Oops, now there's lots of answers that give the whole thing away... Commented Jul 16, 2013 at 1:32
  • I'm sorry, an if statement is a loop now? Commented Jul 16, 2013 at 1:40

5 Answers 5

3

The problem is that you have no loop.

# Write your function below!
 def fizz_count(input):
     count = 0
     for x in input: # you need to iterate through the input list
         if x =="fizz":
             count = count + 1
     return count

There is a more concise way by using the .count() function:

def fizz_count(input):
    return input.count("fizz")
Sign up to request clarification or add additional context in comments.

2 Comments

would x become a list in the for loop ? I am confused because the instructions said to make a list x as input.
x takes on the value of each element in the list input. In this case, in the first loop iteration, x becomes the first element in the list. In the second loop iteration, x becomes the second element in the list, and so on.
1

Get rid of x = [input], that just creates another list containing the list input.

i think the code above the if loop is fine

ifs don't loop; you're probably looking for for:

for x in input:  # 'x' will get assigned to each element of 'input'
    ...

Within this loop, you would check if x is equal to "fizz" and increment the count accordingly (as you are doing with your if-statement currently).

Lastly, move your return-statement out of the loop / if-statement. You want that to get executed after the loop, since you always want to traverse the list entirely before returning.

As a side note, you shouldn't use the name input, as that's already assigned to a built-in function.

Putting it all together:

def fizz_count(l):

    count = 0  # set our initial count to 0

    for x in l:  # for each element x of the list l
        if x == "fizz":  # check if x equals "fizz"
            count = count + 1  # if so, increment count

    return count  # return how many "fizz"s we counted

Comments

0
def fizz_count(x):                       #DEFine func
    count = 0                            #set counter to zero
    for item in x:                       
        if item == "fizz" :               
            count += 1                   #iterate counter +1 for each match
    print count                          #print result
    return count                         #return value 
fizz_count(["fizz","buzz","fizz"])       #call func 

Comments

0

Try this:

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

Comments

-2
def fizz_count(input)
    count = 0
    for x in input:
        count +=  1 if x=="fizz" else 0
    return count

3 Comments

You could make this working by changing count += x=="fizz" ? 1 : 0 to count += (x=="fizz"). If it is True, it is evaluated as 1, else 0. To make it more readable, however, you can change it to: count += 1 if x=="fizz" else 0.
I would prefer a simple if-statement to both of those alternatives.
Sorry guys, I'm always surprised when I rediscover that Python lacks the Ternary Operator.

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.