Properly formatted multiplication table

How can I make a multiplication table that would be organized into a neat table? My current code is:

n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1,n+1): for col in range(1,n+1): print(row*col) print() 

This correctly multiplies everything, but has a list form. I know that I need to enclose it in space, but I'm not sure where this is happening?

+6
source share
13 answers

Quick way (maybe too much horizontal space):

 n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1,n+1): for col in range(1,n+1): print(row*col, end="\t") print() 

The best way:

 n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1,n+1): print(*("{:3}".format(row*col) for col in range(1, n+1))) 
+8
source

Gniebler's approach is pretty elegant. I went to the forefront of building a list of integers using the range function and using the step argument.

at n = 12

 import pprint n = 12 m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1)) pprint.pprint(m) [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36], [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72], [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84], [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96], [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108], [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132], [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]] 

Now that we have a list of list of integers that is in the form that we want, we must convert them to strings that are correctly justified with a width of one larger than the largest integer in the list of lists (the last integer), using the default argument ' ' for fillchar.

 max_width = len(str(m[-1][-1])) + 1 for i in m: i = [str(j).rjust(max_width) for j in i] print(''.join(i)) 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144 

and demonstrate the elasticity of the interval with a different size, for example. n = 9

 n=9 m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1)) for i in m: i = [str(j).rjust(len(str(m[-1][-1]))+1) for j in i] print(''.join(i)) 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 
+4
source

Or you could just do it (not as easy as others, but it works):

 def main(): rows = int(input("Enter the number of rows that you would like to create a multiplication table for: ")) counter = 0 multiplicationTable(rows,counter) def multiplicationTable(rows,counter): size = rows + 1 for i in range (1,size): for nums in range (1,size): value = i*nums print(value,sep=' ',end="\t") counter += 1 if counter%rows == 0: print() else: counter main() 
+1
source

this one looks pretty neat:

  print '\t\t\t=======================================' print("\t\t\t\tMultiplication Tables") print '\t\t\t=======================================\n' for i in range(1,11): print '\t', i, print print("___________________________________________________________________________________________________________________") for j in range(1,11): print("\n") print j, '|', for k in range(1,11): print '\t', j * k, print("\n") 
+1
source

Here's my trick for organizing the output:

 for row in range(1, 11): for col in range(1, 11): num = row * col if num < 10: blank = ' ' # 2 blanks else: if num < 100: blank = ' ' # 1 blank print(blank, num, end = '') # Empty string print() # Start a new line 
+1
source

Creating an arithmetic table is much simpler, but I thought I should post my answer, despite the fact that there are so many answers to this question, because no one was talking about the limit of the table.

User input as a whole

 num = int(raw_input("Enter your number")) 

Set the table limit, to what extent we want to calculate the table for the desired number

 lim = int(raw_input("Enter limit of table")) 

Iterative calculation starting at index 1

In this, I use slicing with the format to adjust the space between the number ie, {: 2} for the two space settings.

 for b in range(1, lim+1): print'{:2} * {:2} = {:2}'.format(a, b, a*b) 

Final CODE:

 num = int(raw_input("Enter your number")) lim = int(raw_input("Enter limit of table")) for b in range(1, lim+1): print'{:2} * {:2} = {:2}'.format(a, b, a*b) 

OUTPUT:

 Enter your number 2 Enter limit of table 20 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 2 * 11 = 22 2 * 12 = 24 2 * 13 = 26 2 * 14 = 28 2 * 15 = 30 2 * 16 = 32 2 * 17 = 34 2 * 18 = 36 2 * 19 = 38 2 * 20 = 40 
+1
source

To do this, print as follows

  print "%d X %d"%(row, col) 

It will print as 2 X 3.

0
source

You can accomplish the effect you are looking for much more easily by placing one of the loops inside the print call.

 n = int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1, n+1): print('\t'.join(str(row * col) for col in range(1, n+1))) 

This creates a generator that gives the string values row*1 , row*2 , ... row*n , combines each of these values ​​with a tab character, and passes the resulting string to print() .

0
source

Your problem is that printing adds a new line and you do not need all these lines.

One way to do this is to build a line for each line, and then print the entire line in a single print statement. A.

 n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1,n+1): s = '' for col in range(1,n+1): s += '{:3} '.format(row * col) print(s) 

The magic is in the bit '{:3} '.format . This is complicated, so here is the tutorial: http://ebeab.com/2012/10/10/python-string-format/

Here's the code in action:

 Please enter a positive integer between 1 and 15: 4 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 
0
source
 n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(1,n+1): for col in range(1,n+1): print(row*col, "\t",end = "") print() #the "\t" adds a tab each time, and the end = "" prints your string horizontally. 
0
source

This is very suitable for a standard multiplication table, which is easy to explain in terms of coding for beginners:

 x = 12 y = 12 print ' ', for fact in range(1, x+1): str(fact).rjust(6), for fact in range(1, y+1): print print fact, for i in range(1,y+1): product = i * fact print str(product).rjust(5), print 
0
source
 for x in range(1, 11): for y in range(1, 11): z = x * y print(z, end="\t") print() #creates the space after the loop 

The above code will give the following result:

0
source

Also:

 table = 12 for i in range(1,11): print(i*table) 
-2
source

Source: https://habr.com/ru/post/959440/


All Articles