FAQ: Learn Python: A Day at the Supermarket - Lists + Functions

What is the reason that we put the “return count” at the same indentation level as the “for?” What would happen if we put “return count” at the same level as the “if” statement?

I don’t believe the lessons have taught us on the proper usage of the “return” statement to close out “if” and “for”

The return has nothing to do with the loops, they’d automatically close out once finished unless you deliberately left them early.

The return is used inside functions to return a value from the function back to the original caller and to terminate execution of the function at that point. That is, once return is used the function exits instantly (barring certain constructs like finally).

If you put return partway through a loop then you may exit the function only partway through your loop (perhaps even on the first iteration). Occasionly you might want to do this early e.g. if you find the condition you want but in many cases that’s a mistake. In this case where you’re trying to make a count/sum of something (which requires every iteration of the loop to complete or you’d only sum some of the values).

1 Like

help

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

i keep getting a blank screen and i’m not able to move on ;n;
i hate this lesson

in Python 3, its
print(fizz) like a function