4/13 Python, Error, 'return' outside function


#1

Here is my code,

mew = ['fizz', 'buzz']
count = 0
def fizz_count(mew):
count = 0
for fizz in mew:
if fizz == 'fizz':
count = count + 1
return count
print count

Please tell me what I am doing wrong! :cat2:


#2

Hi there,

Not sure if you figured it out already but you're printing out the wrong variable. Here's my code and it seemed to work:

mew = ['fizz', 'buzz']
print fizz_count(mew)

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


#3

return sends information to the function without printing it. When you call that function, the computer sees it as the returned value once you have run the function. You need to print whatever you are trying to do rather than return it if it is outside of a function. If it is inside of a function, you need to indent it properly so that it is recognized as in the function.


#4

Also, please format your code so that people can see the issues better. You can use this either with `, with four spaces before EACH line, or by highlighting the text and clicking the </> button at the top of the typing window.

-Edit-

The first way to do it is called "in-line" and is what allows me to do this. You use the left most button on the keyboard just under escape and you put it before and after the text that you want to format


#5

try this:

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


#6

return outside function implies wrong indentation


#7

Didn't work for me, it says that it expects an indented block


#8

please send me a screenshot of your error


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

hopefully this helps


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

hopefully it helps