Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   for and while loops (http://www.velocityreviews.com/forums/t359155-for-and-while-loops.html)

 kydavis77@gmail.com 06-28-2006 09:52 PM

for and while loops

i was wondering if anyone could point me to some good reading about the
for and while loops

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. Write another program
that reads 100 numbers from the user and prints out the sum. "

but im not quite grasping those functions..

please bear im mind i am an extreme newbie at this...thanks in advance

 Bayazee 06-28-2006 10:19 PM

Re: for and while loops

hi

#Exercise 1 :
s=0
while 1:
s+=input("Enter a num : ")
if s>=100:
print "The sum is greater than 100 : ",s
break

#Exercise 1 :
s=0
for i in range(5):
s+=input("Enter num #%d > "%(i+1))
print "The Sum is : " , s

 Bayazee 06-28-2006 10:19 PM

Re: for and while loops

hi

#Exercise 1 :
s=0
while 1:
s+=input("Enter a num : ")
if s>=100:
print "The sum is greater than 100 : ",s
break

#Exercise 2 :
s=0
for i in range(5):
s+=input("Enter num #%d > "%(i+1))
print "The Sum is : " , s

 =?ISO-8859-1?Q?Sch=FCle_Daniel?= 06-28-2006 10:26 PM

Re: for and while loops

kydavis77@gmail.com schrieb:
> i was wondering if anyone could point me to some good reading about the
> for and while loops
>
> 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. Write another program

the hidden hint here is ... "read until"
you can't know ahead how many numbers it will be, the pattern in this
case is to use "while sum smaller then 100"
sum = 0
while sum < 100:
sum = sum + input("more numbers please: ")

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

here you know that you are going to read exactly 100 numbers
sum = 0
for i in range(100):
sum = sum + input("please number #%i: " % (i+1))

the only unclear point here is range(100)
it generates a list with number [0,1,2 ... 99]
and iterates through it
one could write it like
for i in [0,1,2,3,4]:
do_something_with(i)

but it gets tedious to write such a long list

>
> but im not quite grasping those functions..
>
> please bear im mind i am an extreme newbie at this...thanks in advance
>

hth, Daniel

 Simon Forman 06-28-2006 10:46 PM

Re: for and while loops

kydavis77@gmail.com wrote:
> i was wondering if anyone could point me to some good reading about the
> for and while loops
>
> 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. Write another program
> that reads 100 numbers from the user and prints out the sum. "
>
> but im not quite grasping those functions..
>
> please bear im mind i am an extreme newbie at this...thanks in advance

while loops test a condition every time through the loop and keep
running as long as it's true.

for loops run through a finite (usually) set of things and do something
"for" each thing.

Check out one or more python tutorials such as
http://docs.python.org/tut/tut.html (and especially this part
http://docs.python.org/tut/node6.html), a quick search an google can
turn up more.

(Hint: you're probably gonna want to use a while loop for the first
program and a for loop for the second.)

Hope that helps,
~Simon

 Bruno Desthuilliers 06-29-2006 02:02 AM

Re: for and while loops

kydavis77@gmail.com 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

 Bruno Desthuilliers 06-29-2006 03:40 AM

Re: for and while loops

Bayazee a écrit :
> hi
>
> #Exercise 1 :
> s=0
> while 1:
> s+=input("Enter a num : ")
> if s>=100:
> print "The sum is greater than 100 : ",s
> break

Why do you manually check the condition when the construct is meant to
take care of it ?

the_sum = 0
while the_sum < 100:
try:
sum += int(raw_input("please enter a num: ")
except ValueError:
pass

> #Exercise 1 :
> s=0
> for i in range(5):

Was supposed to be 100, not 5.

> s+=input("Enter num #%d > "%(i+1))

idem as above. Using input() is usually a bad idea:

>>> import sys
>>> s = input("please hack my machine: ")

please hack my machine: sys.path.insert(0, '/path/to/malicious/stuff')
>>> sys.path[0]

'/path/to/malicious/stuff'
>>>

 All times are GMT. The time now is 02:44 PM.