Python Forum
Multiplication Table number margins
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplication Table number margins
#1
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()
Reply
#2
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()
Reply
#3
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
Reply
#4
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()
Reply
#5
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©
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiplication Table code alexsendlegames100 3 1,316 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 1,935 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,629 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  Creating table in MySQL db with decimal number issue dangermaus33 7 4,777 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
  List conversion and multiplication johnkyp 5 3,103 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 1,829 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,122 Oct-08-2019, 04:10 AM
Last Post: doug2019
  multiplication by successive addition Zebrol 1 3,471 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  Tracing a multiplication table w/ Python trace() NationalRex22 0 1,728 Jun-11-2019, 03:31 AM
Last Post: NationalRex22
  float multiplication - unexpected output inesk 3 3,254 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020