Why is this code still returning "None" instead of the correct number?


#1

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

according to the error, the correct answer is 1, but this returns "None" despite adding 1 to the count variable.
Also: attempting to use x = 'fizz' results in an incorrect syntax error.


#2

Two issues...

count + 1 is an expression. count += 1 is a statement.

return should not be inside the if statement.


#3

like this?

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

#4

Yes, so long as your indentation is correct. We cannot tell without the code formatted.


#5

Oops, try again. fizz_count(['fizz', 'buzz']) returned 0 instead of the correct answer: 1


#6

That brings us to the other problem... Missing loop. x is a list, so we need to iterate over it.

for n in x:
    if n == 'fizz':

and so on.


#7

implemented loop, still returning either 'None' or 0

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

#8

return should be in neither the if or the for code blocks.


#9

Shifting it still results in 0, or an error stating that the return is out of the function

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

def fizz_count(x):
count = 0
for fizz in x:
if x == 'fizz':
count += 1
return count = File "python", line 7
SyntaxError: 'return' outside function


#10

We cannot return a statement. Format your code and be sure the indentation is correct throughout.


#11

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