Tally votes from an input file?
<p>I'm trying to use python to create a program that can tally votes using an input file.</p>

<p>Say the input file contains:</p>

<pre class="lang-none prettyprint-override"><code>Abby 10 Bob 3
Abby 5 Cathy 7
Cathy 2
Bob 1
Abby 22
</code></pre>

<p>I would want the program to be able to tally the numbers linked to their key (the name of the candidate) and then output from highest voted for to least voted for.</p>

<p>So an example output of:</p>

<pre class="lang-none prettyprint-override"><code>Abby 37
Cathy 9
Bob 4
</code></pre>

<p>I was told I could use a looping mechanism but I am unsure how to do this in python.</p>

<p>My attempt is here:</p>

<p><a href="http://i.stack.imgur.com/1bn30.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/1bn30.jpg" alt="screenshot of my code"></a></p>

<p>It looks like you're using <code>+</code> where you want <code>+=</code>:</p>

<pre><code>if voteinfo[i] in candidates:
  candidates[voteinfo[i]] + int(voteinfo[i+1])
</code></pre>

<p>Should be:</p>

<pre><code>if voteinfo[i] in candidates:
  candidates[voteinfo[i]] += int(voteinfo[i+1])
</code></pre>

python loops dictionary file-io