[Tutor] Getting total of a list (newbie)

Andrei Kulakov ak@silmarill.org
Tue, 11 Sep 2001 12:45:53 -0400


On Tue, Sep 11, 2001 at 09:37:01AM -0400, Brmfq@aol.com wrote:
> Good Day,
> 
> I'm running Python 1.5.2 on win16 (going to upgrade both soon). I've been 
> working on an exercise of writing a program that reads 100 numbers from the 
> user and prints out the sum. Here's the best I've come up with:
> 
> 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, but it's only for 10 numbers. Could someone please show me a 
> better way? Thanks in advance to anyone who replies.
> 
> Joe

total = 0
while 1:
    ans = raw_input("Enter number: ")
    if not ans: break
    total = total + int(ans)
print "Total is", total

> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org