Why does my fizz_count return 1 instead of the correct answer?

Question

Why does my fizz_count return 1 instead of the correct answer?

Answer

Assuming the rest of your code is correct, the issue here is that your function returns too soon. If you put your return statement inside of your for loop, it can only possibly run one time.
Remember, a function exits immediately when it executes a return statement and returns whatever value is there. Take a look at the code snippets below:

total = 0
for item in my_list:
  if item == 2:
    total += 1
  return total  # return is indented to be inside of the for loop!

To fix this, return should be written on the same indentation level as the for loop so that it doesn’t execute it until the loop is done looping, like this:

total = 0
for item in my_list:
  if item == 2:
    total += 1
return total  # doesn’t return until AFTER the loop
12 Likes

Does anyone know why this is returning 3 and not 2? I’m totally confused how just by adding an or statement it seems to throw everything off.

Even when I changed by list items to [“fitz”, “yellow ftzz”, “pink frzy” ] while keeping everything else the same it still prints 3.

When I remove the or statement from the formula and just have if item == “fizz”: it prints 1 as expected…

CODE:

def fizz_count(x):
count = 0
for item in x:
if item == “fizz” or “yellow fizz” :
count = count + 1
return count

type_fizz = [“fizz”, “yellow fizz”, “pink fizy” ]
winner = fizz_count(type_fizz)
print winner

CONSOLE

3

1 Like

or is an operator, not a statement. You can for example not put an if-statement in place of or

I suggest implementing or as a function yourself in order to make yourself consider what its behaviour is and how that is different from what you want. Or, indeed, consider what behaviour or would have to have in order to do what you want there. You can also consider what the order of operations is there, and you can print out results of individual operations (you have multiple operations there, perhaps you should be looking at them one at a time instead, separated from each other)

Or indeed, how does this behave?

5 == 5 + 3

(note its similar structure)

The bottom line here is that you wrote something in English. There shouldn’t be an expectation for it to be meaningful unless you can argue for it from what you know about the individual operations there, similar to:

is it half past noon?

Which also isn’t python
It’s slightly different, because your code does do something, but you haven’t got a good argument for that it does what you want just like how I don’t have an argument to that is it half past noon? would be valid python

As for what exactly it does do, that is something you can figure out by considering what behaviour or does have, and what will therefore happen

1 Like

My above post is mostly about that there shouldn’t be an expectation for it to work, not so much about why that particular case is wrong.

The expectation is the real problem, the actual scenario itself is trivial, just a symptom.

Your expression:

item == "fizz" or "yellow fizz"

Has to be evaluated in some order because there are two operators there. If you apply parenthesis and evaluate it in that order, you’ll find it won’t do what you want. Doesn’t even matter which order you pick, neither works.

 actual order:
(item == "fizz") or "yellow fizz"

also wouldn't work though:
item == ("fizz" or "yellow fizz")
4 Likes

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

my code is what the solution is and its still giving me an error. wtf?

check your indentation
The return should be outside the loop.

1 Like