npv thrilok
npv thrilok

Reputation: 23

how to print for loop output on same line after each iteration

Code

a = int(input("enter a no"))
b = int(input("enter a range"))
for i in range(1, a+1):
    print(i)

    for j in range(1, b + 1):
        c = i * j
        print(i, "*", j, "=", c)

Desired output

1               2               3
1 * 1 = 1       2 * 1 = 2       3 * 1 = 3
1 * 2 = 2       2 * 2 = 4       3 * 2 = 6
1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
1 * 4 = 4       2 * 4 = 8       3 * 4 = 12
1 * 5 = 5       2 * 5 = 10      3 * 5 = 15
1 * 6 = 6       2 * 6 = 12      3 * 6 = 18
1 * 7 = 7       2 * 7 = 14      3 * 7 = 21
1 * 8 = 8       2 * 8 = 16      3 * 8 = 24
1 * 9 = 9       2 * 9 = 18      3 * 9 = 27
1 * 10 = 10     2 * 10 = 20     3 * 10 = 30

Upvotes: 2

Views: 3884

Answers (4)

Alberto Pirillo
Alberto Pirillo

Reputation: 178

This is what you are looking for:

a = int(input("enter a no"))
b = int(input("enter a range"))

for i in range(1, a + 1):
    print(i, end="\t"*4)
print("")

for k in range(1, b + 1):
    for j in range(1, a + 1):
        print(j, "*", k, "=", j*k, end="\t"*2)
    print("")

You have to think line-by-line, since you cannot go back in stdout.

  • The first for will fill the first line
  • I modified your nested loop because you were using the wrong variables
  • "\t" is meant to get decent formatting, but it will eventually break if numbers are too big: I believe there is a more refined approach that I am not aware of
  • print("") is just meant to go to the next line

Upvotes: 0

npv thrilok
npv thrilok

Reputation: 23

I corrected it like this. Thankyou guys. just add the end="\t"*4 to fix the alignment issue.

a = int(input("enter a no "))
b = int(input("enter a range "))

for i in range(1, a+1):
    print(i, end="\t"*4)
print("")

for j in range(1, b + 1):
    for i in range(1, a+1):
        c = i * j
        print(i, "*", j, "=", c, end="\t"*2)
    print("")

Upvotes: 0

DarrylG
DarrylG

Reputation: 17156

Approach

  • Approach is use f-strings to left align all the prints into fields of width 20
  • Use fixed field widths since otherwise columns won't line up with single and multi-digit numbers
  • Use print parameter end = '' to avoid carriage returns on after printing a field

Code

a = int(input("enter a no ")) b = int(input("enter a range "))

width = 20
for i in range(1, a+1):
    print(f'{i:<{width}}', end="")       # Print all multipliers on a single row
print("")

for j in range(1, b + 1):
    # Looping over multiplication row
    for i in range(1, a+1):         # Looping through the columns to multipl
        s = f'{i} * {j} = {i*j}'    # Expression for column field
        print(f'{s:<{width}}', end = '') # Print field left aligned to width 20
    print("")                       # New row

Test

enter a no 4
enter a range 10
1                   2                   3                   4                   
1 * 1 = 1           2 * 1 = 2           3 * 1 = 3           4 * 1 = 4           
1 * 2 = 2           2 * 2 = 4           3 * 2 = 6           4 * 2 = 8           
1 * 3 = 3           2 * 3 = 6           3 * 3 = 9           4 * 3 = 12          
1 * 4 = 4           2 * 4 = 8           3 * 4 = 12          4 * 4 = 16          
1 * 5 = 5           2 * 5 = 10          3 * 5 = 15          4 * 5 = 20          
1 * 6 = 6           2 * 6 = 12          3 * 6 = 18          4 * 6 = 24          
1 * 7 = 7           2 * 7 = 14          3 * 7 = 21          4 * 7 = 28          
1 * 8 = 8           2 * 8 = 16          3 * 8 = 24          4 * 8 = 32          
1 * 9 = 9           2 * 9 = 18          3 * 9 = 27          4 * 9 = 36          
1 * 10 = 10         2 * 10 = 20         3 * 10 = 30         4 * 10 = 40    

Upvotes: 0

Lumorti
Lumorti

Reputation: 180

You can tell print to finish with something other than a newline by specifying the "end" argument:

a = int(input("enter a no "))
b = int(input("enter a range "))

for i in range(1, a+1):
    print(i, end="            ")
print("")

for j in range(1, b + 1):
    for i in range(1, a+1):
        c = i * j
        print(i, "*", j, "=", c, end="    ")
    print("")

In this case I split the main loop into two separate loops, the first outputs the top line (1, 2, 3...) with some large spacing, whilst the second then does all of the others with slightly less spacing.

I also switched the order of the later loops since there should be b lines, each with a multiplications, so the b loop needs to be the outer (first) loop.

In both cases an empty print statement is used to output a newline when we need it.

Upvotes: 1

Related Questions