Error with defining functions

Screen Link: Learn data science with Python and R projects

My 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)

def freq_table(list1):
    dict = {} 
    for value in list1: 
        if value in dict: 
            dict[value] += 1
        else: 
            dict[value] = 1
        return dict 
genres_ft = freq_table(genres)
print (genres_ft)

What I expected to happen:
{‘Social Networking’: 167, ‘Photo & Video’: 349, ‘Games’: 3862, ‘Music’: 138, ‘Reference’: 64, ‘Health & Fitness’: 180, ‘Weather’: 72, ‘Utilities’: 248, ‘Travel’: 81, ‘Shopping’: 122, ‘News’: 75, ‘Navigation’: 46, ‘Lifestyle’: 144, ‘Entertainment’: 535, ‘Food & Drink’: 63, ‘Sports’: 114, ‘Book’: 112, ‘Finance’: 104, ‘Education’: 453, ‘Productivity’: 178, ‘Business’: 57, ‘Catalogs’: 10, ‘Medical’: 23}

What actually happened:

{‘Social Networking’: 1}

Hi all, my code is not resulting in an error but it is not giving the right answer i.e. I am not getting the correct frequencies. Is there a problem with my for loop? Any help will be appreciated. Thanks!

2 Likes

The code looks correct. I think, you’ve used a reserved keyword dict. Can you try changing the name and see if it works? You can rename it as frequency_table

Hey thanks for your reply. I tried using a few other names but keep getting the same result.

In your second function, your return statement is indented in the if block.

2 Likes

Thanks!! That fixed it!

1 Like

Glad, it helped. Happy learning. :slight_smile: