Nested For and While Statements

koutoo at koutoo at
Mon Sep 24 22:43:05 CEST 2007

On Sep 24, 3:01 pm, Paul McGuire <pt... at> wrote:
> On Sep 24, 2:28 pm, kou... at wrote:
> > I start my code with some constants then a while statement.  But I
> > have some For statements towards the end within the While statement
> > where I start getting some errors.  I'm hoping I won't have to post my
> > code, but it looks something like this:
> > Import os, string
> > while this:
> >      All Code indented like it should
> >      Last line in While.
> >            For i in range(1)
> >                 class = []
> >                 class2 = []
> >                 For i in range(2)
> >                      Do this
> >                      And this
> >                      And that.
> >                      Next Line hits snag here?  Where should this line
> > be?
> Why are the for loops further indented than the body of the while?  If
> they are part of the while, "For i in range(1)" should line up with
> "Last line in While."  If they are not part of the while, but come
> after, then "For i in range(1)" should line up with "while this:".
> Also, as James Stroud said, your for loop indexes are the same
> variable.
> -- Paul- Hide quoted text -
> - Show quoted text -

My actual code has different variables, but thanks for pointing that
out.  I'm so used to coding in VB, where indenting isn't a problem, so
all this indenting is new to me how Python wants it dictated.  I made
the changes and still get an error at the same point after the second
For statement? So:

While :
     Code under while
     More under while.

     For (x)
          Part of x
          Last of x

               Part of y
               Still part of y
               last part of y.

          New line part of x #where does this belong?

     Last line under while not in For  #Should go under While, right?

If I want all my code to run under the while, then the While statement
should be the only line of code not indented once I state it then,

More information about the Python-list mailing list