4.Lists + Functions


I have n o idea what's wrong.If i have a list shouldn't the exercise instructions say so?
Anyway this is the error :File "python", line 6
count = count += 1
SyntaxError: invalid syntax

# Write your function below!
def fizz_count(x):
    count = 0
    for x in fizz_count:
        if x == "fizz":
            count = count += 1
    return count


You can use:

count = count + 1


count += 1

But not both together.


Yes, you have both on the same line.


Doent work.New error : Oops, try again. an exception was raised for fizz_count(['fizz', 'buzz']): 'function' object is not iterable


Lol, I should've seen this line.


I don't understand.Help


for x in fizz_count:

lets see, fizz_count is the function, lets loop over the function? I would loop over the list: x (the parameter if the function):

for y in x:


Correct but it doesn't print anything in the console.BTW in order to print something in the console i must add a list right?

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

.Why does it work now(before and yesterday it didn't.And the day before yesterday)?
And how should i know that i had to add a list in order to print something?Or shouldn't i?Im' confused


It does if you call print on the function call, or you store the result in a variable, and print the variable (which it seems you did)

In this case? yes.

You looped over your function, that doesn't work. So i guess that is why it didn't work earlier. Can't be sure

The code in your function is written to receive a list? Using a for .. in loop is used for lists. You can also write it different, so it would work for strings


Yes but in this particular exercise i don't have to write a list right?If i want to only?


No, you don't have to write the list. The exercise will test a list, but it is nice to see for yourself what you achieved :slight_smile:


Aha ok.Thank you very much.I was stuck BTW with the same code for days.And today it worked.Thanks


I am trying to add this code learned from this website to my python IDLE; however, for some reason it does not print. Can anyone help me out with this please! much appreciated!

for word in "Mahir Sultanov":
	if word == "Mahir":

P.S I try all forms of printing

print (Mahir)
print Mahir


Take a look at this code:

for word in "Mahir Sultanov":

its letter gets printed (the loop goes over each letter), a letter can never equal a whole name. If had a list:

for word in ["Mahir", "Sultanov"]:
	if word == "Mahir":

it should work. So, looping over a list with for in will give you the items in the list, looping over a string with for in will give you each letter


@orujgadimov A for/in loop doesn't work on a single string, you have to split it first.


It did. Thank you, dantemp and stetim94, for your prompt response. Much APPRECIATED!!!!


its wrong thats y its not work you print the result after the return fiction the programming goes through top to bottom when its come to fizz there is no list then it gave you an error bcs thers no list