Here is a link to tweets code: http://pastebin.com/iHWaTkjj

Here is output excerpt (colors not replicated below):

(0, 'The spotlight has finally been put on the low-life leakers! They will be caught!')
(-3, 'Leaking, and even illegal classified leaking, has been a big problem in Washington for years. Failing @nytimes (and others) must apologize!')
(3, 'Stock market hits new high with longest winning streak in decades. Great level of confidence and optimism - even before tax plan rollout!')
(-1, 'Venezuela should allow Leopoldo Lopez, a political prisoner & husband of @liliantintori (just met w/ @marcorubio) o… https://t.co/HhgE51dtSN')

My output is identical to staff solution except for the parentheses. Any idea where these are getting into the printed output?

1 Answer 1


If the word is neither positive nor negative, there is nothing to do (delete that else:), storing score and tweet somewhere should entirely happen after examining all the words in a given tweet. Otherwise, your reported score would be wrong if last word was not neutral.

With the line

scores = (score, tweet)

you create a tuple of score and tweet, which is then printed using those parentheses, and commata between the tuple elements. In the final loop, you could use something like

score = "{:2d} {}".format(final_score[i][0], finalscore[i][1])

to manually format it in a nicer way (2d means decimal number with at least 2 digits, padded with spaces if necessary, did that for nicer alignment of multiple tweets)

  • Okay. I fixed everything and my code works perfectly. Can you walk me through the line of code so that I make sure I understand what it is doing? Commented Mar 3, 2017 at 0:53
  • String method format will replace all pairs of {} with its arguments. If you specify a number like {0}, it will take the in this case first argument (zero-based indexing) for that placeholder, else those are used in the order of appearance. Also, you can specify a formatting like 2d, separated from the index with a colon (even if there's no index). 2d would be about equivalent to %2d in C's printf. The final_score[i][0] then is the first element of tuple final_score[i]. Tuples in Python are something like immutable lists.
    – Blauelf
    Commented Mar 3, 2017 at 10:37
  • Excellent! Just what I needed, it totally makes sense now. Commented Mar 3, 2017 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .