Hi am new to python
Nassim Gannoun
elcarga at hotmail.com
Wed Sep 9 00:44:26 CEST 2015
Hi I'm also new to Python but would like to reply.
Like others have stated there is a built in function (sum) that can give the sum of the elements of a list, but if what you are trying to do is learn how to use the while command here is some code that will work.
Your question:
My question is in a while loop; how do l sum all the numbers in the given list (list_a)?
list_a = [8, 5, 2, 4]
sum_a = 0 # for storing the sum of list_a
i = 0 # for looping through the list_a# Use a while loop to sum all numbers in list_a# If you store the sums into sum_a
print(sum_a) # should print 19
My answer:
list_a = [8, 5, 2, 4]
sum_a = 0
i = 0
while i < (len(list_a)):
sum_a += list_a[i]
i += 1
print(sum_a)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150908/4c500db5/attachment.html>
More information about the Python-list
mailing list