Python Forum

Full Version: Multiplication Table number margins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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()
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
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()
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©