-3

I am trying to count the word fizz using python. However it is giving me an error.

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

item= ["fizz","cat", "fizz", "Dog", "fizz"]

example= fizz_count(item)

print example

i checked with indentation but still it does not work. Where i am doing wrong?

3
  • What is the error? Python's error messages are usually very helpful. Commented Nov 5, 2016 at 22:16
  • the error is - File "python", line 8 SyntaxError: 'return' outside function Commented Nov 5, 2016 at 22:18
  • 3
    Your indentation is the problem. Commented Nov 5, 2016 at 22:20

5 Answers 5

1

Your indentation seems to be incorrect, and you should not have the first return count (why would you return count as soon as you define it??).

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

item = ["fizz", "cat", "fizz", "Dog", "fizz"]

example = fizz_count(item)

print example
Sign up to request clarification or add additional context in comments.

Comments

1

Please try the following code: remove return count right after count = 0

There are also a few indentation changes.

def fizz_count(x):
    count = 0

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

item = ["fizz","cat", "fizz", "Dog", "fizz"]

example = fizz_count(item)

print example

Comments

1

the problem is the identation in your line return

Try with this:

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

Comments

1

You do not need the first 'return' statement in your code. It works as follows, with indentation and spacing fixed:

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

item= ["fizz","cat", "fizz", "Dog", "fizz"]

example = fizz_count(item)
print example

5 Comments

the first return count was a mistake , i removed it but still there is a problem.
Did you make sure to indent your remaining return statement? Your code works without the first return statement, from there you will get an error if you don't make sure you're indenting correctly. Indentation matters in Python; try running your code exactly as I have it written here (four spaces or one tab indent before the return statement, so it is within your fizz_count function).
I am working with codecademy python IDE, i checked with indentation. It is not working.
Can you let us know what specific error you're getting (does it tell you which line)? A lot of online learning tools have very fickle IDEs, sensitive to any problem in spacing. There may also be a bug--you can check the codecademy forums to see if others encounter a problem (I would refresh or restart the lesson entirely for a clean interpreter state). If you open a Python shell in your terminal (go to Terminal on your computer and type 'python' at the command line) and test your code, it works with the single return statement and spacing and indentation improved.
Hurray! It must have been a spacing issue.
0

Well i am new to python world. What i learned is return statement should be some thing like this.

Example one :-

def split_train_test(data, test_ratio):
    shuffled_indices = np.random.permutation(len(data))
    test_set_size = int(len(data) * test_ratio)
    test_indices = shuffled_indices[:test_set_size]
    train_indices = shuffled_indices[test_set_size:]
    return data.iloc[train_indices],data.iloc[test_indices]

Example two :-

def load_housing_data(housing_path=HOUSING_PATH):
    csv_path = os.path.join(housing_path, "housing.csv")
    return pd.read_csv(csv_path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.