Number 4


#1

my code is right but i get an error:
Code

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

but the error says:

File "python", line 6
SyntaxError: 'return' outside function

can anyone help me out?


#2

Make sure you're defining a function and not calling a function on the first line. fizz_count(x) is how you would call the function - defining the function fizz_count(x) has a different syntax.


The instructions say

Create a variable count to hold the ongoing count. Initialize it to zero.

You have to make sure you do this for the function to work properly.


if x == "fizz": is comparing the whole list (that is passed as an argument to the fizz_count function) to "fizz". x should instead be compared to each individual item in the list.


count = "fizz" + 1 is trying to add the number 1 to the string "fizz". What should be happening here instead is, 1 should be added to the count each time "fizz" is found in the list.

Hopefully this is helpful!


#3

Question states this to do...

Write a function that counts how many times the string "fizz" appears in a list.

It simply means we will be making Counter function that counts how many times word 'fizz' comes in a list.
Lets see your code.

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

You missed a colon.
update**
if x == "fizz": # Comparing a list with the 'fizz'?
count = "fizz" + 1 # We can't add string with numbers? right!
But over all this code fails to solve actual problem. so How to go about it ?

Lets make a simple word structure to solve this problem.

  1. We make a function that takes list as argument.
    fizz_count(x):
    Did! what now?

  2. We have to count number of times fizz occurs inside the list.
    How can we save this information?where?
    To deal this we create a simple variable at top of of our function.
    count= 0

  3. Now we have to go over each item of list > It means we have loop over it.
    You will do this

  4. We will use conditional statement (if statement) to check if any item matches with 'fizz'.
    You will do this by finding the logic

  5. If we find an item then what ?
    We increase the value of counter by 1.

count = count + 1 (Inside if conditon)

Don't forget to return count variable!
I hope it helps!:slight_smile:


#4

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