(Nov-14-2019, 04:26 AM)Than999 Wrote: My question is how do I iteratively sum up all my numbers in a list, without the use of the special function sum()
Just iterate over items in list and add to total (sum means adding all items; do you want sum only positive values?):
>>> nums = [11.5, 28.3, 23.5, -4.8, 15.9, -63.1, 79.4, 80.0, 0, 67.4, -11.9, 32.6] >>> total = 0 >>> for num in nums: ... total += num ... >>> total 258.8NB! Never use sum as variable name. You overwrite built-in function and it will come and 'bite you when you least expect'
EDIT: for only positive numbers value checking must be added:
>>> nums = [11.5, 28.3, 23.5, -4.8, 15.9, -63.1, 79.4, 80.0, 0, 67.4, -11.9, 32.6] >>> total = 0 >>> for num in nums: ... if 0 <= num: ... total += num ... >>> total 338.6
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.