Totally stuck on Functions Fundamentals 8.Writing a Single Function

I am totally lost on this question on 3 fronts, and I feel like a complete idiot (now doubting everything I have done up to this point as I am totally blanking on writing anything past a basic function in the function section… even forgetting the basics on how to write a for loop…):

  1. What is the difference between my code and the answer? It says it’s wrong
  2. Why are we using value=row[index]. I get that value is what we use to build the frequency table.
  3. Why is it freq_table(7)?

I would use Python Tutor to visualize, but it’s pulling from the CSV, so I can’t.

[https://app.dataquest.io/m/315/functions%3A-fundamentals/8/writing-a-single-function](https://Screen Link:)

My Code: ‘’'opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def freq_table(index):
frequency_table={}
for row in apps_data[1:]:
value = row[index]
if value in frequency_table:
frequency_table[value]+=1
else:
frequency_table[value]=1
return frequency_table

ratings_ft=freq_table(7)’’’

What I expected to happen: To get the correct answer

What actually happened: I got an error

ratings_ft is shorter than we expected.

Correct code
‘’'opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def freq_table(index):
frequency_table = {}

for row in apps_data[1:]:
    value = row[index]
    if value in frequency_table:
        frequency_table[value] += 1
    else:
        frequency_table[value] = 1
        
return frequency_table

ratings_ft = freq_table(7)’’’

Hi @hills.mike91,

  1. It seems that you have an issue with the indentation of the return statement. It should have the same indentation as the for statement:
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def freq_table(index):
    frequency_table={}
    for row in apps_data[1:]:
        value = row[index]
        if value in frequency_table:
            frequency_table[value]+=1
        else:
            frequency_table[value]=1
    return frequency_table

ratings_ft=freq_table(7)
  1. Here we are creating a function to generate a frequency table for any column of given dataset. Index, in this case, is the index of the column for which we want to create the frequency table. We pass this index as an argument in our function. value=row[index] means that we extract a value of this column for each row for inserting it in our frequency table.
  2. Here we want to create this table for the column with the index 7.
2 Likes

@ hills.mike91
plz make your code indented at first before posting or you can post the image (screenshot of your code) that will be more understandable.