# [Tutor] (no subject)

**Daniel Yoo
**
dyoo@hkn.EECS.Berkeley.EDU

*Wed, 30 Aug 2000 18:55:01 -0700 (PDT)*

>* "Write a program that continually reads in numbers from the user and adds
*>* them together until the sum reaches 100".
*>*
*>* Seems simple, but I can't get it. Any help?
*
This message will go through the steps in building this program, so don't
read the whole thing if you want to try your hand at it. I find that
building up a partial solution, and working on that, is sometimes easier
than going top-down.
Let's first build a frame for this program. The code that corresponds to:
"continually [read numbers] until the sum reaches 100" fits naturally with
the idea of a 'while' loop --- repeat a behavior, until a certain
condition is met:
###
while sum < 100:
# we'll need to fill this in with some sort of action
###
We need to define what "sum" is. Let's set it, at the beginning, to zero:
###
sum = 0
while sum < 100:
# we'll need to fill this in with some sort of action
###
So we have that. Next, we need to work on the part that "reads numbers
from the user." This can be done using either raw_input() or input().
For this instance, using input() will be easier, because we're working
strictly with numerical input. Returning to our 'while' loop:
###
sum = 0
while sum < 100:
number = input("Please enter a number: ")
###
If we try running the program at this point, it will never get out of the
loop, because sum will always be less than 100. We see the need to add
our number to the sum we have so far.
###
sum = 0
while sum < 100:
number = input("Please enter a number: ")
sum = sum + number
###
This fragment does what you want --- it adds numbers up until it hits a
sum of 100. It would be nice to see a running total of the sum, just to
see it working. A small print statement can be added in the beginning of
the while loop:
###
sum = 0
while sum < 100:
print "Sum so far:", sum
number = input("Please enter a number: ")
sum = sum + number
###
I hope this helps!