for and while loops

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

  1. kydavis77

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

  2. kydavis77

    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. Advertisements

  3. kydavis77

    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. 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: ")

    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
    hth, Daniel
     
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Jun 28, 2006
    #4
  5. kydavis77

    Simon Forman Guest

    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 :
    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)

    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 !-)
    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
    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 :
    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

    Was supposed to be 100, not 5.
    idem as above. Using input() is usually a bad idea:
     
    Bruno Desthuilliers, Jun 29, 2006
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.