[Tutor] while and for

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 10 Oct 2000 01:20:06 +0200


On Mon, Oct 09, 2000 at 01:31:43PM -0400, CMNOLEN@aol.com wrote:
> I asked earlier for guidance in writing a program to total input from 100 
> entries. It  worked great. Now I want the program to contain an 'Enter 0 to 
> quit' line. Also I would like a running count of the entries to show in the 
> 'The sum is now' line. I am stuck. I guess these questions are so basic most 
> print doesn't answer them?I am stuck! 
> 
> I am using:
> #########################################################
> total = 0
> for  i in range(100):
>     total = total + input("Please enter your next number: ")
>     print "The sum is now", total
> #########################################################

To break out of a loop, you use 'break'. Something like:

total = 0
for i in range(100):
   inp = input("Please enter your next number, or 0 to exit: ")
   if inp == 0:
      break
   total = total + inp
   print "The sum is now", total
   
Should work.

Remco Gerlich