How to do the break??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fullmoon84
    New Member
    • Sep 2006
    • 9

    How to do the break??

    i doing a proram where i will first ask the user to key in the datas they have, Lets 3, then i will on ly allow them to key in 3 datas.

    after that , ithe 3 datas will be add together, giving the total sum automatically.

    This is the program that i write:

    total=0
    aCounter=0
    a=raw_input("in sert a:\t")
    a=float(a)
    while a!=-1:
    total=total+a
    aCounter=aCount er+1
    a=raw_input("in sert a:\t")
    a=float(a)
    if aCounter!=0:
    add=float(total )/aCounter
    n=float(n)/aCounter
    print"total X ia",add
    if a==n:
    break

    but when i run , ii state there " break outside loop",. Can anyone tell me how to do?? thank you.
  • Bellum
    New Member
    • Sep 2006
    • 23

    #2
    The actual program is tabbed, isn't it? Could you do that, I can't really tell whats going on otherwise. >.>


    Well, I don't know. I might still not be able to tell what's going on, but it will help.

    Comment

    • fullmoon84
      New Member
      • Sep 2006
      • 9

      #3
      thanks for ur help.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        In future, please read guidlines so your posts look like this:

        Code:
        total=0
        aCounter=0
        nDataPoints = int(raw_input("How many data points?:\t"))
        
        while 1:
            aCounter = aCounter + 1
            a = float(raw_input("insert data point:\t"))
            total = total + a
            if aCounter == nDataPoints:
                break
        average = total/aCounter
        print "Total = %f, Average = %f:" %(total, average)
        Otherwise, nobody can see the structure of you code.

        Comment

        • fullmoon84
          New Member
          • Sep 2006
          • 9

          #5
          ok.Thank you

          Comment

          • Jmlman
            New Member
            • Oct 2006
            • 2

            #6
            I think that the break command is not working because it is placed inside an if statement, whereas it should be placed in a loop.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by Jmlman
              I think that the break command is not working because it is placed inside an if statement, whereas it should be placed in a loop.

              A better way to do that is:

              Code:
              for i in range(nDataPoints):
                  a = float(raw_input("insert data point:\t"))
                  total = total + a
              you can use i which counts for zero to nDataPoints - 1 inside you loop if you need to.

              Comment

              Working...