Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (16.4k points)

How might I make a multiplication table that is coordinated into a perfect table? My present 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 effectively multiplies everything except for has it in a list structure. I realize I need to settle it and space it appropriately, however, I don't know where that goes?

1 Answer

0 votes
by (26.4k points)

For quick way:

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()

For better 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)))

Using python f-strings:

for row in range(1, n + 1):

    print(*(f"{row*col:3}" for col in range(1, n + 1)))

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 29, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Aug 17, 2019 in Java by Anvi (10.2k points)
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...