Summing List Elements
<p>I'm trying to do this question for this online coding course I'm part of, and one of the questions requires me to add together integers in a list. I've tried to find the answer (and visited a few other pages on this site), but I can't think of anything. Help please!</p>

<p>Here's my code so far:</p>

<pre><code>total = 0
att = input("RSVPs: ")
att = att.split(",")
for i in att:
  print(sum(iatt) for i in att)
</code></pre>

<p><a href="http://i.stack.imgur.com/H0ZBM.png" rel="nofollow"><img src="http://i.stack.imgur.com/H0ZBM.png" alt="enter image description here"></a></p>

<p>Your error is caused because you provide sum with an integer value (<code>iatt = int(i)</code>) when you should be providing it with the contents of the list which is split on <code>','</code>.</p>

<p>You have a couple of options for this. Either provide a comprehension to <code>sum</code> and cast every element to an <code>int</code> inside the comprehension:</p>

<pre><code>print(sum(int(i) for i in att))
</code></pre>

<p>or, use a built-in like <code>map</code> which pretty much does the same thing:</p>

<pre><code>print(sum(map(int,att)))
</code></pre>

<p>in both cases, <code>sum</code> expects something that can be iterated through and it handles the summing.</p>

<p>Of course, you could manually loop over the contents of <code>att</code>, adding <code>int(i)</code> to <code>total</code> as you go:</p>

<pre><code>for i in att:
    total += int(i)
print(total)    
</code></pre>

python list python-3.x