Nested loops confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Graham

    #1

    Nested loops confusion

    Hi,

    I expect this is very obvious for anyone who knows what they're doing -
    but I don't understand what's the problem with the following code. I
    was intending that the program cycle through all i and j (ie. all
    possible (i,j) coordinates, but the output when I run the program shows
    me up to

    "1
    99
    plot 3
    1
    100
    plot 2
    done j"

    and doesn't perform the print functions for any i > 1.

    Help much appreciated! :)

    Matt


    corna = int(raw_input(" CornA? "))
    cornb = int(raw_input(" CornB? "))
    side = int(raw_input(" Side? "))
    i = 0
    j = 0
    for i in range(100):
    for j in range(100):
    x = corna + i * side / 100
    y = cornb + j * side / 100
    c = int(x^2 + y^2)
    if c%3 == 1:
    print i
    print j
    print "plot 1"
    elif c%3 == 2:
    print i
    print j
    print "plot 2"
    elif c%3 == 0:
    print i
    print j
    print "plot 3"
    print "done j"
    print "Done i"


  • Matthew Graham

    #2
    Re: Nested loops confusion

    Oops, I forget to reset the j after the inner loop. Always manage to
    work these things out just after asking for help! ;-)

    Matthew Graham wrote:[color=blue]
    > Hi,
    >
    > I expect this is very obvious for anyone who knows what they're doing -
    > but I don't understand what's the problem with the following code. I
    > was intending that the program cycle through all i and j (ie. all
    > possible (i,j) coordinates, but the output when I run the program shows
    > me up to
    >
    > "1
    > 99
    > plot 3
    > 1
    > 100
    > plot 2
    > done j"
    >
    > and doesn't perform the print functions for any i > 1.
    >
    > Help much appreciated! :)
    >
    > Matt
    >
    >
    > corna = int(raw_input(" CornA? "))
    > cornb = int(raw_input(" CornB? "))
    > side = int(raw_input(" Side? "))
    > i = 0
    > j = 0
    > for i in range(100):
    > for j in range(100):
    > x = corna + i * side / 100
    > y = cornb + j * side / 100
    > c = int(x^2 + y^2)
    > if c%3 == 1:
    > print i
    > print j
    > print "plot 1"
    > elif c%3 == 2:
    > print i
    > print j
    > print "plot 2"
    > elif c%3 == 0:
    > print i
    > print j
    > print "plot 3"
    > print "done j"
    > print "Done i"
    >
    >[/color]

    Comment

    • Matthew Graham

      #3
      Re: Nested loops confusion

      Thanks very much for the advice, have tidied it up and tested and seems
      to be working as needed. I'm still not sure what was stopping the inner
      loop from working earlier - but removing the redundancy in "j=0" and so
      on seems to have solved it.

      Matt

      Dennis Lee Bieber wrote:[color=blue]
      > If that worked, you've got some weird code -- and it isn't what was
      > posted... Zeroing "j" does NOTHING, since the for loop assigns all
      > values to it, from 0..n-1
      >
      > for loop indices do not need to be pre-initialized.[/color]

      Comment

      • John Machin

        #4
        Re: Nested loops confusion

        On 11/05/2006 5:59 PM, Matthew Graham wrote:[color=blue]
        > Thanks very much for the advice, have tidied it up and tested and seems
        > to be working as needed.[/color]

        Seems to be working? Consider where you have the expression x^2 + y^2
        .... I'd like to bet that you mean "x squared" etc, not "x exclusive-or
        2" etc.
        [color=blue][color=green][color=darkred]
        >>> x = 3
        >>> x ^ 2[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> x ** 2[/color][/color][/color]
        9[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        [color=blue]
        > I'm still not sure what was stopping the inner
        > loop from working earlier[/color]

        Call me crazy, but I thought you said it was the outer loop that wasn't
        working ...

        [color=blue]
        > - but removing the redundancy in "j=0" and so
        > on seems to have solved it.
        >[/color]

        There's that "seems" word again. Removing "j=0" should have had no
        effect at all, as Dennis has pointed out. What meaning do you attach to
        "working"? Have you written any tests? Have you contemplated dropping
        the size to say 5x5 instead of 100x100 and hand-calculating the expected
        results?

        Comment

        • conor.robinson@gmail.com

          #5
          Re: Nested loops confusion

          >I'm still not sure what was stopping the inner[color=blue]
          >loop from working earlier - but removing the redundancy in "j=0" and so
          >on seems to have solved it.[/color]

          Call me crazy, but be careful when programming python in different text
          editors and in general, ie cutting and pasting, tabing and spacing.
          Loops can look fine and not work (try moving around test print
          statements for iterators), in this case try re-tabing your indents so
          that everything is uniform in the editor you are using.

          Comment

          • Matthew Graham

            #6
            Re: Nested loops confusion

            John Machin wrote:[color=blue]
            > Seems to be working? Consider where you have the expression x^2 + y^2
            > ... I'd like to bet that you mean "x squared" etc, not "x exclusive-or
            > 2" etc.[/color]

            You're right, and I had already changed it after a look through a python
            tutorial told me my mistake.
            [color=blue]
            > There's that "seems" word again. Removing "j=0" should have had no
            > effect at all, as Dennis has pointed out. What meaning do you attach to
            > "working"? Have you written any tests? Have you contemplated dropping
            > the size to say 5x5 instead of 100x100 and hand-calculating the expected
            > results?[/color]

            True, I'm being sloppy, will test it properly when I have the time.

            Comment

            • Edward Elliott

              #7
              Re: Nested loops confusion

              conor.robinson@ gmail.com wrote:[color=blue]
              > Call me crazy, but be careful when programming python in different text
              > editors and in general, ie cutting and pasting, tabing and spacing.
              > Loops can look fine and not work (try moving around test print
              > statements for iterators), in this case try re-tabing your indents so
              > that everything is uniform in the editor you are using.[/color]

              the -tt flag is a good way to catch/avoid such problems:
              #!/usr/bin/python -tt

              Comment

              Working...