[EMAIL PROTECTED] a écrit :
> i was wondering if anyone could point me to some good reading about the
> for and while loops

There's not much to say.

while <expr>:
   <block>

will execute <block> as long as <expr> is True.

for <item> in <sequence>:
   <block>

will execute <block> for each <item> in <sequence>.

ie :
for letter in ["a", "b", "c"]:
   do_something_with(letter)

is equivalent to

letter = "a"
do_something_with(letter)
letter = "b"
do_something_with(letter)
letter = "c"
do_something_with(letter)


> i am trying to write some programs
> "Exercise 1
> 
> Write a program that continually reads in numbers from the user and
> adds them together until the sum reaches 100.

Since it's nearly impossible to predict how much iteration will be 
necessary for this condition to be satisfied[1], you want a while loop. 
The condition is 'the_sum >= 100' (starting with 'the_sum == 0'). The 
body of the loop is mainly : read a number in, add it to the_sum.

[1] FWIW, we have 0 < number of iterations < +infinity, since nothing 
specifies that the user can not enter negative numbers !-)

> Write another program
> that reads 100 numbers from the user and prints out the sum. "

Here you have a definite number of iterations, so it's a clear use case 
for a for loop, which will take care of the loop count by itself. Now 
since the for loop iterates over a sequence, you need such a sequence of 
100 items. The canonical solution is the range(count) function, which 
will produce a sequence of <count> integers. The body of the loop is 
exactly the same as in the previous case.

> but im not quite grasping those functions..

which 'functions' ? 'for .. in ..' and 'while ..' are statements 
(instructions), not functions. A functions is eval'd, and returns a 
value. A statement is executed, and has no value.

HTH
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to