for and while loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kydavis77@gmail.com

    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

    #2
    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

    Comment

    • Bayazee

      #3
      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

      Comment

      • Schüle Daniel

        #4
        Re: for and while loops

        kydavis77@gmail .com schrieb:[color=blue]
        > 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[/color]

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

        [color=blue]
        > that reads 100 numbers from the user and prints out the sum. "[/color]

        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_wi th(i)

        but it gets tedious to write such a long list
        [color=blue]
        >
        > but im not quite grasping those functions..
        >
        > please bear im mind i am an extreme newbie at this...thanks in advance
        >[/color]

        hth, Daniel

        Comment

        • Bruno Desthuilliers

          #5
          Re: for and while loops

          kydavis77@gmail .com a écrit :[color=blue]
          > i was wondering if anyone could point me to some good reading about the
          > for and while loops[/color]

          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_wi th(letter)

          is equivalent to

          letter = "a"
          do_something_wi th(letter)
          letter = "b"
          do_something_wi th(letter)
          letter = "c"
          do_something_wi th(letter)

          [color=blue]
          > 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.[/color]

          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 !-)
          [color=blue]
          > Write another program
          > that reads 100 numbers from the user and prints out the sum. "[/color]

          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.
          [color=blue]
          > but im not quite grasping those functions..[/color]

          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

          Comment

          • Simon Forman

            #6
            Re: for and while loops

            kydavis77@gmail .com wrote:[color=blue]
            > 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[/color]

            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

            Comment

            • Bruno Desthuilliers

              #7
              Re: for and while loops

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

              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

              [color=blue]
              > #Exercise 1 :
              > s=0
              > for i in range(5):[/color]

              Was supposed to be 100, not 5.
              [color=blue]
              > s+=input("Enter num #%d > "%(i+1))[/color]

              idem as above. Using input() is usually a bad idea:
              [color=blue][color=green][color=darkred]
              >>> import sys
              >>> s = input("please hack my machine: ")[/color][/color][/color]
              please hack my machine: sys.path.insert (0, '/path/to/malicious/stuff')[color=blue][color=green][color=darkred]
              >>> sys.path[0][/color][/color][/color]
              '/path/to/malicious/stuff'[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Comment

              Working...