for and while loops
Schüle Daniel
uval at rz.uni-karlsruhe.de
Wed Jun 28 18:26:40 EDT 2006
kydavis77 at 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
More information about the Python-list
mailing list