Getting an error when trying to combine functions

Screen Link:

My 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(column):
    a_sum = 0
    for element in column:
        a_sum += float(element)
    return a_sum

def find_length(column):
    length = 0
    for element in column:
        length += 1
    return length

def mean(data_set,index):
    column = extract(data_set, index)
    return find_sum(column)/find_length(column)
    
avg_price = mean(data_set=apps_data,index= 4)   

What I expected to happen:
I expected the output to give a Mean rating correctly . However ,the answer has shown a different methodology.

Below is the code used in the Answer :slight_smile:

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 didnt understand is why was a_list used as a parameter for the functions find_length and find_sum , since Column could have been used as since the find_sum & find_length functions will add and find the length of the data from the list Column created when defininf the function extract() . I did the same in my code and it gave the correct answer . What could be the reason here for using a different methodology in the answer since it seems a bit complicated . Also , is my code incorrect here despite giving the correct answer ?
What actually happened:

Got the correct answer 

<!--Enter other details below: -->

Hi @RishabhKaul ,

There’s nothing wrong with your code. These are just two different name choices.

The solution use a_list because that’s what the function receives: a list.

You used the name Column and that’s also okay, as long as you’re consistent throughout the function.

I don’t believe you’re getting any errors, are you?

Hey @otavios.s ,

thanks for the quick response . I am not getting any errors . But I have 2 follow up questions regarding my own code - :

  1. When defining the mean function , If i remove the code column = extract(data_set, index) , it throws an error . My question is , ideally shouldnt the code run since column has already been defined by extracting the data and all I am doing is returning the output return find_sum(column)/find_length(column)

  2. What do you suggest is a better form of code . The one that I had entered and got the right answer for OR the one defined in the answer ? Your suggestion would be helpful

When you create a variable inside a function, it only exists inside that function, or in the scope of the function… Therefore, the variable column in the extract function only exists inside this function and cannot be accessed by the mean function.

You can read more about scopes here:

There’s absolutely no difference. As I said, these are just different name choices.

I’m not going to say you can name your variables with any name you want. Although Python won’t stop you, you should not name variables a, b, abcde, ygashjbja, for example. It’s a good practice to choose a readable name that makes sense to what the variable represents.

But aside from that, you can give your variables whatever name you want as long as you’re consistent throughout the code.

@otavios.s What I have understood is that the mean function here has to call the other 3 functions in order to get the correct answer .

Thanks for the reply

1 Like