6. Slicing Lists and Strings


#1


https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/4?curriculum_id=4f89dab3d788890003000096#

File "python", line 7
SyntaxError: 'return' outside function

count the number of "fizz"


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


#2

your for loop is not inside the function, you might want to re-indent the for loop

Also, i would compare x with a string of "fizz", not the functionname fizz_count


#3

def fizz_count(x):
count = 0
for item in x:
if x == "fizz":
++count
return count
like this? I don't think I did it right


#4

sorry, i missed it. It should be item =="fizz",why? If you use a for in loop, item will hold each item in the list as it progresses, which you can see nicely with:

for item in x:
   print item

x is the list, comparing it with the list will do you little good, you want to compare it with the items in the list


#5

I tried what you said

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

but I get the error
fizz_count(['fizz', 'buzz']) returned 0 instead of the correct answer: 1


#6

you want to re-indent your for return line, so it is at the same indent level as your for loop, so the entire for loop can run, and find all instances

python doesn't know a ++, so you will have to do count += 1


#7

thanks so much I'm such an idiot sometimes