Why do I keep getting a syntax error?

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

I keep getting a syntax error for the if x = 'fizz' this am I suppose to write down the whole function or is it a problem with the fizz part I already tried if x = 'fizz': , if x = "fizz": and if x = fizz: I don't what to use I thought it was "fizz" because it was from a list value but I am not sure?


def fizz_count(x):
for item in x:
if item == 'fizz':
return count

Your code should look like this. The reason you're getting a syntax error is because you're trying to print, not return count. Also don't forget about indents! You indent once after count=0, then again after if item=='fizz', and again on the next line, the make the return count inline with the for-loop. I don't know if my code is indented in the comment but that's they way it should be.

Good luck coding!


Hi @bob1352,

You are using the = assignment operator. What you want to use is the == equality operator instead

Here's the gist of it:

# Try reading this as 'x is now 12' 
x = 12
# printing x will confirm this:
print x

# Reassigning a new value to x like so
x = 10
# and printing it will confirm that using the = assignment
# operator changes the value of x
print x

# Now try reading this as `x is it 10?`
x == 10
# printing it will confirm that it is
print x == 10    # yields true

# printing this will also confirm that it is not 12
# and that using the equality operator == does not change the value of x
print x == 12    # yields false

So you just need to change your operator in the condition of your if and as @indianmuffin mentioned, use return instead of print and you should be on your way... :smile:

Good luck!


This is the correct answer. It worked for me. Please see my code below:

def fizz_count(x):
    count = 0
    for word in x:
        if word == 'fizz':
            count = count + 1
    return count
list_1 = ['fizz', 'crap', 'fizz', 'stuck', 'hate', 'fizz', 'damn', 'buzz', 'fizz']
fizz_list = fizz_count(list_1) 
print fizz_list


Hi @ninagt3,

See this other thread to learn the trick and have the code you paste retain indentation and formatting... :smile:


You used the for loop wrong.

your function is acting on the argument/ list called x

and what you want is for each piece of data in the list x, if the piece of data is equal to the string 'fizz', add 1 to the amount of times fizz has been counted

so it would look like:

for piece_of_data in x:
if piece_of_data == 'fizz':
count += 1 #the correct shorthand for adding something to a variable is +=
return count

try that and tell e how it works,.... also, replace piece_of_data to the word item