AJP
AJP

Reputation: 47

Omit last line in nested for loop

I have a nested for loop:

for i in range(0,6): for j in range(0,10): print(j, end="") print("\n",0)

Which results in the following:

0123456789 0 0123456789 0 0123456789 0 0123456789 0 0123456789 0 0123456789 0

How do I omit the last 0 from the result?

Upvotes: 0

Views: 135

Answers (2)

Athena
Athena

Reputation: 543

you can simply add an if as follows:

for i in range(0, 6):
    for j in range(0, 10):
        print(j, end="")
    if i != 5:
        print("\n", 0)

Upvotes: 2

Rockybilly
Rockybilly

Reputation: 4520

You can keep a variable to see if the process is complete and there is no need for more zeroes.

done = False
for i in range(0,6):
    for j in range(0,10):
        print(j, end="")
        if i == 5 and j == 9:
            done = True
    if not done:
        print("\n",0)

    # You can also use this variable to break the loop
    # after the process is complete.

Upvotes: 2

Related Questions