for and while loops

Discussion in 'Python' started by kydavis77@gmail.com, Jun 28, 2006.

  1. Guest

    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
     
    , Jun 28, 2006
    #1
    1. Advertising

  2. Bayazee Guest

    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, Jun 28, 2006
    #2
    1. Advertising

  3. Bayazee Guest

    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
     
    Bayazee, Jun 28, 2006
    #3
  4. 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
     
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Jun 28, 2006
    #4
  5. Simon Forman Guest

    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
     
    Simon Forman, Jun 28, 2006
    #5
  6. 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, Jun 29, 2006
    #6
  7. 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'
    >>>
     
    Bruno Desthuilliers, Jun 29, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angie

    while loops

    Angie, Jul 30, 2003, in forum: C++
    Replies:
    4
    Views:
    371
    Angie
    Aug 1, 2003
  2. cman

    Testing pointers in while-loops

    cman, Mar 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    327
    Chris Thomasson
    Mar 3, 2007
  3. Zachary  Turner

    Variable declaration and while loops

    Zachary Turner, Jun 18, 2007, in forum: C++
    Replies:
    10
    Views:
    558
    Andre Kostur
    Jun 18, 2007
  4. Replies:
    33
    Views:
    992
    Andrew Tomazos
    Jul 27, 2009
  5. Me
    Replies:
    2
    Views:
    247
Loading...

Share This Page