No error in code, but didn't get the answer. Wondering what is wrong

Screen Link: Learn data science with Python and R projects

My Code:

def get_key_list_dict(di, title):
    dict_by_title = {}
    for k,v in di[title].items():
        if v not in dict_by_title:
            dict_by_title[v] = [k]
        else:
            dict_by_title[v].append(k)
    return dict_by_title


def get_avg(di, title_as_key_dict):
    for k,v in title_as_key_dict.items():
        final_list = [0 for i in range(3)]
        for i in v:
            final_list[0] += di['total_bill'][i]
            final_list[1] += di['tip'][i]
            final_list[2] += di['size'][i]
        res[k] = [final_list[i]/len(v) for i in range(3)]
    return res

def avg_group(di, ke):
    res = {}
    a = get_key_list_dict(di,ke)
    return get_avg(di,a)

What I expected to happen:
Expected answer to be correct. Perhaps it has to do with rounding?

What actually happened:

did not return the expected value
```<br><br><a href="https://app.dataquest.io/m/1020/screen/7" target="_blank">Click here to open the screen in a new tab.</a>

Your current code will throw an error because you are trying to use res as a dictionary in your get_avg() function but don’t have it initialized in that function.

I would suggest sharing the code that works if the Grader says your working code isn’t returning the expected answer.

Thank you so much! This solved my problem.

In my original code, I initiated res as a global variable and it didn’t work.

With your suggestion, I initiated res under get_avg and it worked! :slight_smile:

1 Like