On Tue, 08 Sep 2015 17:44:26 -0500, Nassim Gannoun wrote:
> My question is in a while loop; how do l sum all the numbers in the
> given list (list_a)?
You don't normally use a while loop or a counter to iterate over a list.
Such a question should only be used as a precursor to discussing better
methods of achieving the required result.
While loop:
> sum_a = 0
> i = 0
> while i < (len(list_a)):
> sum_a += list_a[i]
> i += 1
> print(sum_a)
Better, use a for loop:
s = 0
for i in list_a:
s += i
print (s)
Even better, use sum():
s = sum(list_a)
print (s)
And if you only want to display the answer:
print (sum(list_a))
--
Denis McMahon, [email protected]
--
https://mail.python.org/mailman/listinfo/python-list