Learn
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.

Instructions

1.

Add the compare() function from the example above to your application. It should take two arguments: dict1 and dict2. Place it after the flatten() function.

Click Run to save your code.

Folder Icon

Take this course for free

Already have an account?