My answer works, but am I wrong?


My Code: 

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

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

genres = extract(11)

def freq_table(extract):
    frequency_table = {}
    for element in genres:
        if element in frequency_table:
            frequency_table[element] += 1
        else:
            frequency_table[element] = 1
    return frequency_table

genres_ft = freq_table(genres)

print(genres_ft)

Answer key code:

# CODE FROM THE PREVIOUS SCREEN
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

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

genres = extract(11)
# SOLUTION CODE
def freq_table(column):
    frequency_table = {}    
    for value in column:
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1            
    return frequency_table

genres_ft = freq_table(genres)

Question:

-Both answers work, but is my code inefficient in any way since it is not the same as the answer key?

Glad I finally got mine to work and didn’t look at the answer key to find the solution before I could come up with one, but bummed my answer wasn’t the same as the answer key.

Hello please include the mission link

1 Like

You’re not wrong, your answer It’s actually the same as the solution code, the difference is that you used a different name for the iterators in the loop. You used

And the solution code says

Wich is exactly the same because the syntax is For iterator in sequence
Also, sometimes your answers are going to be different and that’s ok, everyone has a different code style and what matters is the answer
Good luck!

1 Like

Hello,

The problem is in the freq_table function. You defined it with the solo parameter extract, but then you did not use this parameter inside the function. Instead, you used genres.

You can name the parameter whatever you want to, but then you have to stick to that name in the whole function. If, as in this case, the parameter is a list through which your code will iterate, then you must use the parameter name to iterate. If you set the parameter as extract then you have to use for (anythinghere) in extract. When you use genres, the function uses the genres variable you defined earlier.

Your function is not generalist as the solution function, as it will only work with the genres column. What you need to do is to iterate over the parameter you defined (extract, as I said) and pass the list as an argument when you call the function. Also, it is a good practice to name the parameter as something that makes sense in the context you’re in. That’s why column is a better name than extract.

2 Likes

I guess my thought process was this,

-When I was defining my function “freq_table” I thought that the parameter “extract” was actually pointing to my variable “extract” in the earlier function. So what I thought originally would happen is that I could use only one function to get two answers. I thought if I printed “freq_table(6)” what that would actually do, is it would go through the function “extract” first and enter an argument of 6. Then after that function of “extract” completes the genres variable would now change to whatever list that index 6 goes to. Then from there the freq_table functions would use that list and move forward.

I’m now seeing, not only was I over complicating that. But I can’t use just one of these functions to create a frequency table, they both have to work in conjunction. I learned on the next page how to create one function that creates a frequency table.

Any how I’m going to read over your answers a bit more to really get a grasp of what you’re saying. Appreciate the insight and help everyone is always prompt and helpful on here.

2 Likes

You don’t have an extract variable in your code, just a function. In fact, you can use one function to get two answers, all you need to do is to call the extract function from inside the freq_table function. To do this, freq_table would receive an index number. This is how it would look like:

def freq_table(index):
    column = extract(index)
    frequency_table = {}    
    for value in column:
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1            
    return frequency_table

Note how the first line in the freq_table calls the extract function. Then you use this result in the rest of the function. Two functions are being used, but you only have to call one of them.

2 Likes

Ok that makes sense thanks! Last question I hope, in the answer:

def freq_table(column):

Is that “column” actually calling the “column” variable from the first solution? Or is this just a name to keep things in proper order?

I’m not sure I understood the question, but this column is the parameter of the function. You can name it whatever you want, but it is a good practice to choose a name that makes sense to what you’re doing. What you pass as an argument to this parameter is the column you extracted with the extract function.

Sorry I didn’t make it very clear, I believe you answered my question though. I was wonder when you make the second function the is the parameter “(column)” actually calling the variable “column” from the first function, or was it simply a name for the parameter to keep things in order. I believe this is just a name though like you said good practice to keep the parameter name in line with what you are doing.

No, it does not call variable from the extract function. The variables instantiated inside a function, only exist inside that function.