Python Forum
Multiplication Table number margins - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Multiplication Table number margins (/thread-21174.html)



Multiplication Table number margins - CJ707 - Sep-18-2019

Need to know how to format a 12*12 multiplication table to show a multiplication table chart with margins like this:
X 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12

The code to produce the table that fits in the margins above is below:
def main():
 
    rows = int(input("What is the upper bound of multiplication table? "))
    print("The multiplication table for 2 to", rows)
    print()
    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()



RE: Multiplication Table number margins - luoheng - Sep-18-2019

What's the problem? The code works well.
Here is an elegant way to show the table:
def main():
    rows = int(input("What is the upper bound of multiplication table? "))
    print("The multiplication table for 2 to", rows)
    print()
    counter = 0
    multiplicationTable(rows,counter)
  
def multiplicationTable(rows,counter):
    size = rows + 1
    for i in range(1,size):
        print(*(i*nums for nums in range(1, size)), sep='\t')
main()



RE: Multiplication Table number margins - CJ707 - Sep-18-2019

Yes, but I'm needing to add one new row on top and a new column on the left of the table with an X in the upper left corner.
X 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12


RE: Multiplication Table number margins - luoheng - Sep-18-2019

This code may help you.
def main():
    rows = int(input("What is the upper bound of multiplication table? "))
    print("The multiplication table for 2 to", rows)
    print()
    counter = 0
    multiplicationTable(rows,counter)
   
def multiplicationTable(rows,counter):
    size = rows + 1
    # header
    print("X", *range(1, size), sep='\t')
    for i in range(1,size):
        print(i, *(i*nums for nums in range(1, size)), sep='\t')
main()



RE: Multiplication Table number margins - CJ707 - Sep-18-2019

Is there a way to do it using nested for loops?

for i in range (1, 13):
for j in range(1, 13):
c = (i*j)
Multi_table.append©