Gaining Insights
Code Consolidation

In this unit, we’ll be adding the code that will analyze the Twitter data we retrieve from Twitter users.

At the moment, however, our Celebrity Match application can be tough to read or understand for other developers. Let’s start by making our code easier to use (and easier to read).



First, delete the following line of code from CelebrityMatch.py:

handle = "@Codecademy"

Note: You may previously used another Twitter username. The example above uses Codecademy’s Twitter username.


Under the line of code that assigns the personality_insights variable, analyze the body of text that we’ve retrieved from Twitter (the tweets) by adding:

pi_result = personality_insights.profile(text)

On the next line, return pi_result. Use the example above to help you.


Move all of the previous code (excluding the package imports) into a new function called analyze(). The function should accept one parameter, handle.

Use the following example to help you:

def analyze(handle): #This code should include the Twitter credentials, IBM credentials, #statuses, and Personality Insights results pi_result = personality_insights.profile(text) return pi_result

Important: Python cares about whitespace, so remember to properly indent the code inside of the analyze() function. It will save you time later on!

If you’re having trouble with indentation, you can take a look at the indentation in the analyze() method present of this exercise. If you decide to use it directly, however, be careful - there are some minor differences in the analyze() code you’

Folder Icon

Take this course for free

Already have an account?