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

You have not defined total

Had the same so I used a text editor to check the indents.

Hey, the exercise itself is going to input the ‘[“fizz”,“cat”,“fizz”]’ inside of your “x”.
Also, you have a mistake in your for loop. Check the code below to see if you can identify your mistake.

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

My code works for this exercise (ran it on my own computer with a local terminal), but I’m not getting the result display… this is my code:

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

fizz_count(["hello", "my", "name", "fizz", "is", "fizz"])

The lesson has been marked as correct, but this is the result I’m getting:

I’m on a chrome web browser, and I’ve tried reloading/restarting the browser… is this happening to anyone else?

1 Like

Why can’t I use this function (fizz_count) to count the instances of “fizz” inside a string?
My code is:
image
and it gives 0 as an output

looping over a string gives one character a time, simple to demonstrate with a simple print statement:

for item in x:
   print item

a single letter will never equal multiple letters/a whole word.

1 Like

The code still has issue as in the past from 2018 -2020 and to pass this execise you should copy the code provided in the solution. :expressionless:

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

fizz_counted = [“fizz”, “cat”, “fizz”]
counted = fizz_count(fizz_counted)
print(counted)

Output
1 :no_pedestrians: instead of 2

What is this code? Is that what you wrote? Or?

The code below is from the module.

The code you posted is the provided solution? I find that difficult to believe

Where is the exercise url in this FAQ? can’t find it, could you provide it?

Here is the screenshot of the webpage with required url, exercise and solution. I hope you are able to make out the content of the screenshot.

So I now have to type the entire url? Please copy paste the url to the forum, makes access a lot easier.

If there is indeed a problem in the solution, having the exercise url makes reporting the bug a lot easier

But feels more like the code you wrote, contains a mistake. Your return is nested within your for loop, so the moment a match has been found, return keyword is reached, which will signal that the function is done executing

def count_small(numbers):
total = 0
for n in numbers:
if n < 10:
total = total + 1
return total

lotto = [4, 8, 15, 16, 23, 42]
small = count_small(lotto)
print small

This is an example code from the lesson. I don’t understand how
‘numbers’ is being defined here. Seeing as numbers is suppose to be the list/dictionary that the variable n is suppose to be checked for.

If you’re posting code to the forums, please see- How do I format code in my posts? as it makes reading and interpreting your text much easier for everyone else.

In this example the function is defined with one parameter called numbers. When you call your function here you pass a single argument, in this case a list [4, 8, 15, 16, 23, 42].

This effectively assigns that object to the name numbers inside the function, equivalent to the following- numbers = lotto.

Each time you call that function with a different argument you assign something different to numbers inside the function (it is forgotten as soon as the function exits).

To be more explicit you can specify the parameter in the function call such that-

small = count_small(lotto)
# is equivalent to-
small = count(numbers=lotto)

numbers is a parameter. A parameter is the variable that is defined within the parenthesis of the function definition. You can name the parameters anything you want excluding keywords such as: def count(foo) or def count(n): In this case numbers is just a parameter that represents the list/dictionary that the variable n is checking. Hope this helps!

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

The problem we all face here is with the indentations.
Here is my solution and it gave me a perfect 3:

def fizz_count(x): count = 0 for item in x: if item=="fizz": count+=1 return count print (fizz_count(["fizz", "cat", "fizz", "fuzz", "fizz"]))