Gaining Insights

The compare() Function

The flatten() function flattens the results from a user and store the results in a dictionary. The next step is to write a function that can compare two dictionaries (the user's and the celebrity's).

To compare them we will start with one of the returned results (let's say your traits) and then compare them with celebrity's traits. Then, we'll compute the "distance" between the traits and finally create a new dictionary that stores the trait and distance.

def compare(dict1, dict2):
    compared_data = {}
    for keys in dict1:
        if dict1[keys] != dict2[keys]:
                compared_data[keys]=abs(dict1[keys] - dict2[keys])
    return compared_data

Again, It's not important to understand exactly how the compare() function works. What's important is to make sure we include it as part of our application so that we can view PI results.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.