Project 1: display_table function not working

Hello

I am struggling to understand the code given to us for the function display_table as I get the following error after running this code:

def freq_table(dataset, index):
    table = {}
    total = 0
    
    for row in dataset:
        count_object = row[index]
        total += 1
        
        if count_object in table:
            table[count_object] += 1
        else:
            table[count_object] = 1
            
    table_percentages = {}
    for key in table:
        percentage = (table[key]/total)*100
        table_percentages[key] = percentage
        
    return table_percentages
        
def display_table(dataset, index):
    table = freq_table(dataset, index)
    table_display = []
    for key in table:
        key_val_as_tuple = (table[key], key)
        table_display.append(key_val_as_tuple)

    table_sorted = sorted(table_display, reverse = True)
    for entry in table_sorted:
        print(entry[1], ':', entry[0])

display_table(google_final,1)

My output is then:

Hey Misha!

Happy to help here — just need a bit more context so I can understand what you were actually passing into the given function. Can you upload your .ipynb file to your response so I can take a closer look?

Best,
Dee

Hi Dustin

Sorry to have troubled - I realised that I needed to rerun a kernel each time I open a project file

Thank you
Misha

1 Like