0

I have the a program to print items from lists within lists in a specific way. Here is that piece of code:

 for y in range(0,5):      
     print '\n'
     for x in tableau:
          if y < len(x):
           print x[y],
          else :
              print '   ' 

What I want is for the if statement go back to the inner loop(for x in tables) after it executes the print ' ' in the else part of if statement. Is there any way to do that?

4
  • 6
    Did you try break? Commented Nov 19, 2015 at 10:32
  • What you want seems quite unclear, do you want to exit the inner loop in the case it executes "print ' ' " ? Commented Nov 19, 2015 at 10:48
  • yeah and go back to the for x in tableau part Commented Nov 19, 2015 at 10:53
  • You should explain what is the attended result... Commented Nov 19, 2015 at 10:56

3 Answers 3

1
 for y in range(0,5):      
 print '\n'
 for x in tableau:
      if y < len(x):
       print x[y],
      else :
          print '   '
          break 

Will break out of the inner for and back into the outer for, after the print is executed. This will print a "\n" and then move back to the inner for, which I believe is what you are asking?

Sign up to request clarification or add additional context in comments.

Comments

1

As I don't really understand what you need, I suggest you 2 solutions
First one: print ' ' instead of inexistent list element (3 times if len(x) == 2)

for x in tableau:
    print '\n'
    for y in x:
        print y
    for y in range(5 - len(x))
        print '   ' 

Second one: print ' ' always at the end of the list

for x in tableau:
    print '\n'
    for y in x:
        print y
    print '   ' 

Comments

1

As suggested by Delgan, the answer is staightforward - you have to use the break keyword :

 for y in range(0,5):      
     print '\n'
     for x in tableau:
          if y < len(x):
           print x[y],
          else :
              print '   ' 
              break

The break keyword exits the most inner loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.