Functions Fundamentals #11

https://app.dataquest.io/m/315/functions%3A-fundamentals/11/combining-functions

Your Code: Enclose your code in 3 backticks like this to format properly: your code

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

def extract(data_set, index):
    column = []    
    for row in data_set[1:]:
        value = row[index]
        column.append(value)    
    return column

def find_sum(a_list):
    a_sum = 0
    for element in a_list:
        a_sum += float(element)
    return a_sum

def find_length(a_list):
    length = 0
    for element in a_list:
        length += 1
    return length
def mean(data_set, index):
    column=extract(data_set, index)
    return find_sum(column) / find_length(column)

avg_price = mean(apps_data,4)

What I expected to happen:
I am not really sure. I was stuck, and the hints did not help so I broke down and look at the answer.

Where I am confused is that there is a list named column, but in the mean functionhas variable named column, but that is the name of the list. I renamed the items in the return row and the lesson still let me pass.
Would we want to do something like this in practice? Why are these two items named the same?
If I wanted to print(column) it did not seem to print anything.

Thank you.

Hi @anthonyealdea!

You are asking why column is used as a variable name in both the mean() and the extract() function definitions, am I right?

As you noticed yourself, you can rename both (or one) of these variables and your code will still run correctly. The reason for this is something called scope. Any variable defined inside a function is a local variable. If you try to access it from outside the function - that is, the global scope - just like you did when you tried to print(column), you’ll see that that’s not possible.

Each variable named column exists only in its own local scope - just like Paris, Texas and Paris, France are two different cities.

Let me know if this cleared up things for you a little or if you’d like me to elaborate on this a bit more.

Thank you very much. I did not think about scope since I am still so new to coding. Do you think that this is a best practice? Should there be different variable names?