[SOLVED]Lists + Functions - Traceback error


#1



Hello all, I seem to be stuck on lists + functions. After reviewing information here on the forums I can still not find where my code is incorrect. Any assistance is most helpful.

Here is the link to the exercise I am stuck on:

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

Error is as follows:

Traceback (most recent call last):
File "python", line 4, in
NameError: name 'x' is not defined


This code:
Should add +1 to count per 'fizz' present


# Write your function below!
def fizz_count(x):
    count = 0
for count in x:
    if count == 'fizz':
        count =+1
print count


#2

That is because the statement is out of the function block where x is defined. Add four spaces of indentation to these lines:

for count in x:
    if count == 'fizz':
        count =+1
print count

#3

Well that got it to run, however I got this error message now:

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


#4

To add to cgraffeo's response -- there is another bug in here as well. You are using the same name for two different variables.

count = 0: this represents an integer, a counter of the number of "fizz" strings you are finding
for count in x: here, count is getting set to the strings in the array you passed in as x

The simple solution is just to rename "for count in x" to "for some_string in x"; then you would replace "if count" with "if some_string".


#5

We were coming to this one, but glad you pointed it out. Many hands makes the work light.


#6

The error message has the clue: returned which suggests the program should be returning a value, not printing it.

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

print fizz_count(['fizz', 'buzz'])    # 1

#7

# Write your function below!
def fizz_count(x):
    count = 0
    for item in x:
        if item == 'fizz':
            count =+1
    return count
    
print fizz_count(["fizz","cat","fizz"])

is still returning Oops, try again. fizz_count([5, 'fizz', 'fizz', 'fIzZ', 10, 'fizz']) returned 1 instead of the correct answer: 3

Did I just miss something here?


#8

My bad... should be += 1. Fixed my example.


#9

Haha oh my goodness, that was an error from my origional code I first posted.

Silly me.

Thanks so much for all your help that seems to have gotten it!