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?
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 ' '
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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.