List and Function. need explanation. thanks


#1

Question: why both functions work but first one can't pass.

**def fizz_count(fizz):**
**    count = 0**
**    for item in x:**
**        if item == str('fizz'):**
**            count +=1**
**    return count**
**x = ['fizz','buzz', 'fizz']**
**qt = fizz_count(x)**
**print qt**


**def fizz_count(x):**
**    count = 0**
**    for item in x:**
**        if item == str('fizz'):**
**            count +=1**
**    return count**
**x = ['fizz','buzz', 'fizz']**
**qt = fizz_count(x)**
**print qt**

#2

Hello, @prmishania!
It's true that both of your codes work but the difference with them is that on the first one you're not using the argument fizz, that holds the reference to your list named x, but the list "x" itself.

"But how is that possible?"
Your list x is what we call as global.
Since it was not declared inside any other function, you'll be able to use it at any part of your code but if you tried:

qt = fizz_count(x)
x = ['fizz', 'buzz', 'fizz']

It wouldn't work because list x wasn't declared yet.
Check this post, if you'd like to know more about how a function can see a global structure/variable.

But okay, your first code wasn't accepted? Maybe it didn't follow some specific rules about the names of arguments or something, that can happen.


#3

Hi @prmishania and @g4be ,

Yes, both functions will produce the correct output when they are submitted to Codecademy, because they both work with the same list, x.

But as @g4be noted, the first code example does this by ignoring the function parameter, fizz, and using the global name, x, to access the list. The second code example accesses the list, x, by passing that name to the function when it calls it, and then referring to the list by the parameter name, fizz, inside the function. The second example represents the correct way to do it.

The function in the first example will not produce the intended result if it is called with an argument other than x, because it will ignore the argument and work with x instead.

Here's an example ...

def fizz_count(fizz):
    count = 0
    for item in x: # ignores the parameter, fizz
        if item == str('fizz'):
            count +=1
    return count

x = ['fizz','buzz', 'fizz']
y = ['fizz', 'fizz', 'buzz', 'buzz', 'fizz']

qtx = fizz_count(x)
qty = fizz_count(y)

print qtx
print qty

Output ...

2
2

We wanted 3 for the second count, but only got 2. In fact, Codecademy tests your function by passing it its own lists, and that is why the first example does not pass.

Here it is, fixed ...

def fizz_count(fizz):
    count = 0
    for item in fizz: # fixed; uses the parameter, fizz
        if item == str('fizz'):
            count +=1
    return count

x = ['fizz','buzz', 'fizz']
y = ['fizz', 'fizz', 'buzz', 'buzz', 'fizz']

qtx = fizz_count(x)
qty = fizz_count(y)

print qtx
print qty

Output ...

2
3

#4

I forgot to mention that, thanks for telling him @appylpye! :v:


#5

Thanks, that make sense now.


#6

Not only me but @appylpye as well :slightly_smiling:
Glad to help!


#7

Both of you guys were helpful. THanks