4/13 Fizz Count Wrong


#1

Exercise Link:
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/lists--functions?action=lesson_resume&link_content_target=interstitial_lesson

I’m not sure what is wrong with my code. I’ve read around this forum and it seems identical to the answers that have been accepted as correct, but I’m not sure why mine isn’t working. It keeps giving me an error that it is finding 0 or 1, when the correct number is something else (like 2 or 3). Could someone point out what I’m missing?

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

Thanks!


#2

@riadan I am not sure why you are enclosing if statement in () like:

if (word == "fizz"):

I am running it like this, it runs ok:

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

  
print fizz_count(["fizz", "bear", "day", 1, "fizz"])

and getting the expected 2. I also noticed that indentation is a huge deal with python. For instance, if I indent “return count” right beneath “count +=1” then it returns 1 instead of 2.

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

  
print fizz_count(["fizz", "bear", "day", 1, "fizz"])

Hope this helps at all.

Shavkat


#3

Thanks! I agree that the parenthesis don’t matter. The issue was a problem with the indentation. Thanks for pointing that out!


#4

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