How to run 3 loops?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    How to run 3 loops?

    How would I write 3 for loops in the same code?

    Code:
    for i in range(1,11):
             i +=1
             print i
    Fine for one loop, but how do I get 2 more in there? I've tried this:

    Code:
    for i in range(1,11):
            i +=1
    j=1
    for j in range(1,11):
           j +=1
    k =1
    for k in range(1,11):
           k +=1
           print i,j,k
    Doesn't work :( It can run either of this ways

    1 1 1
    2 2 2
    3 3 3, etc

    or this way, which I'd prefer:
    ...
    8
    9
    10
    ....1 <---dots were just needed just to move the columns over in here
    ....2
    ....3...etc
    .........1
    .........2
    .........3..... etc

    maybe something like this:
    Code:
    print i
    if i ==10:
         print j
    if j==10:
         print k
    ???
  • cybervegan
    New Member
    • Jan 2007
    • 36

    #2
    Hiya TheKid,

    Try this:
    Code:
    for i in range(1,11):
        print i
        for j in range(1,11):
            print "\t%i" % j
            for k in range(1,11):
                print "\t\t%i" % k
    You MUST remember to indent each for loop by a consistent amount more than it's enclosing loop. Always be sure to use SPACES or TABS but DO NOT MIX THEM - USE ONE OR THE OTHER. Spaces (usually in blocks of four) are the preferred indentation method. If you mix them, you WILL get confusing errors.

    hth,
    -cybervegan

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      Thank you for the reply and advice. I tried your script and it didn't produce what I was attempting:

      .....
      8
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      9
      1
      2
      3...etc. is how that came out but it using your script and "==" I got it:
      Code:
      [code]

      for i in range(1,11):
      print i
      for j in range(1,11):
      if i==10:
      print "\t%s" % j
      for k in range(1,11):
      if j==10:
      print "\t\t%s" % k

      [\code]

      Your tab ideas works........ Thanks again!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        This may interest you:
        Code:
        def gen(s,e):
            for x in range(s,e):
                yield '%s' % x
            for y in range(s,e):
                yield '\t%s' % y
            for z in range(s,e):
                yield '\t\t%s' % z
        
        for num in gen(1,11):
            print num
        Code:
        def gen(s,e):
            for x in range(s,e):
                yield '%s\n' % x
            for y in range(s,e):
                yield '\t%s\n' % y
            for z in range(s,e):
                yield '\t\t%s\n' % z
        
        nList = []
        for num in gen(1,33):
            nList.append(num)
        
        print "".join(nList)

        Comment

        Working...