[Tutor] Getting total of a list (newbie)

alan.gauld@bt.com alan.gauld@bt.com
Tue, 11 Sep 2001 17:50:17 +0100


Probably stating the obvious but....

> > num = input("Please enter a number:")
> > tot = [num]
> > while len(tot) < 10:
> >      nex = input("Please enter another number: ")
> >      tot.append (nex)
> > print 'total of 10 numbers is',
> > 
> tot[0]+tot[1]+tot[2]+tot[3]+tot[4]+tot[5]+tot[6]+tot[7]+tot[8]+tot[9]
> This works for an arbitrary list or tuple of numbers:
> 
> print reduce(operator.add, tot)

Only replaces the summing line not all the input stuff!
The input stuff is fine as it is, except you should probably 
use raw_input() instead of input() - its safer from abuse...

An alternative is:
total = 0
for item in tot:
   total = total + item
print total

Alan g