-1

I want to write a Multiplication table program. My problem is that the program cannot print to the right side.

The program will ask the user for start and stop. For example:

    start_number = 2
    stop_number = 4

Output should be like this:

2 x 1 = 2    3 x 1 = 3    4 x 1 = 4
2 x 2 = 4    3 x 2 = 6    4 x 2 = 8
2 x 3 = 6    3 x 3 = 9    4 x 3 = 12
2 x 4 = 8    3 x 4 = 12   4 x 4 = 16
2 x 5 = 10   3 x 5 = 15   4 x 5 = 20
2 x 6 = 12   3 x 6 = 18   4 x 6 = 24
2 x 7 = 14   3 x 7 = 21   4 x 7 = 28
2 x 8 = 16   3 x 8 = 24   4 x 8 = 32
2 x 9 = 18   3 x 9 = 27   4 x 9 = 36
2 x 10 = 20  3 x 10 = 30  4 x 10 = 40
2 x 11 = 22  3 x 11 = 33  4 x 11 = 44
2 x 12 = 24  3 x 12 = 36  4 x 12 = 48

This is my code :

start = int(input("Enter start number : "))
stop = int(input("Enter stop number : "))
def multi_num(start,stop):
    for i in range(start, stop+1):
        for j in range(1, 13):
            print("%d x %d = %d" % (i, j, i * j))
multi_num(start, stop)

This is my output:

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48

I tried to use end=" " but it's not working - the program prints output in one line:

2 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 =  142 x 8 = 162 x 9 = 182 x 10 = 202 x 11 = 222 x 12 = 243 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 273 x 10 = 303 x 11 = 333 x 12 = 364 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 364 x 10 = 404 x 11 = 444 x 12 = 48
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    You'll have to format it yourself -- refer to [loops - Printing a table in Python - Stack Overflow](https://stackoverflow.com/questions/43244717/printing-a-table-in-python) for an example. – user202729 Jan 20 '21 at 05:13

2 Answers2

1

Try the following code:

start = int(input("Enter start number : "))
stop = int(input("Enter stop number : "))
def multi_num(start,stop):
    for j in range(1, 13):
        line = ['{} X {} = {}'.format(n, j, n*j) for n in range(start,stop+1)]
        print('\t'.join(line))
multi_num(start, stop)
anurag
  • 1,715
  • 1
  • 8
  • 28
1

I tried to use end=" " but it's not working

It's definitely working though: end=" " will suppress the newline used by default, so no newline => everything is on the same line. It does exactly what it's supposed to.

The actual problem is that you're thinking in columns but terminals work line by line: you print a line, go to the next one, print the next line, ..., like a tape or typewriter (which is very much what they were, the original computer IO were a souped up typewriter for input and a line printer outputting continuous form paper for output).

You could munge your output so that it's generated by column then the columns are pasted together before printing, or switch the terminal to raw mode and manage the terminal as a TUI.

But the simplest solution is to work your thinking around what the terminal wants instead of what you expect, and perform line-wise generation: switch your two loops around so that every i is used for each j, generating a full line, then add a newline before getting to the next j:

start = int(input("Enter start number : "))
stop = int(input("Enter stop number : "))
def multi_num(start,stop):
    for j in range(1, 13):
        for i in range(start, stop+1):
            print(f"{i} x {j} = {i*j}", end='\t')
        print()
multi_num(start, stop)

result:

Enter start number : 2
Enter stop number : 5
2 x 1 = 2   3 x 1 = 3   4 x 1 = 4   5 x 1 = 5   
2 x 2 = 4   3 x 2 = 6   4 x 2 = 8   5 x 2 = 10  
2 x 3 = 6   3 x 3 = 9   4 x 3 = 12  5 x 3 = 15  
2 x 4 = 8   3 x 4 = 12  4 x 4 = 16  5 x 4 = 20  
2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  
2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  
2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  
2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  
2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  
2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 
2 x 11 = 22 3 x 11 = 33 4 x 11 = 44 5 x 11 = 55 
2 x 12 = 24 3 x 12 = 36 4 x 12 = 48 5 x 12 = 60 
Masklinn
  • 34,759
  • 3
  • 38
  • 57