1

Instructions: Write a program that generates the multiplication table for numbers 1-10. Use two for loops to complete your program. You will need to put one for loop inside of the other for loop. As an extra challenge, see if you can get the indention to look correct. So the output of your program should be:

enter image description here

My Code:

# Start a for loop from 1 to 10.

for num1 in range(1, 10):

# Start a nested loop from 1 to 10.

    for num2 in range(1, 10):

    # Display the multiplication for each value of num.

        print('%4d'%(num1*num2), end = '')

# Display new line after inner for loop finished.

print()

The numbers need to align like in the picture above in the instructions. Mine just shoot straight across the screen. I'm not sure what I am doing wrong.

Here is what mine look like:

enter image description here

6
  • You say "Display new line after inner for loop finished." and then have a print() statement underneath. Can you explain what the purpose of that print() statement is? Commented Jun 8, 2020 at 0:09
  • @Ann The editing is in error. This shows the desired output instead of the actual output under "Here's what mine look like:" Commented Jun 8, 2020 at 0:11
  • fixed incorrect edit Commented Jun 8, 2020 at 0:15
  • Sorry, I couldn't find the other image. Commented Jun 8, 2020 at 0:15
  • Why not just print ('\n') after the second loop? Commented Jun 8, 2020 at 0:16

5 Answers 5

1

You can do it this way:

for num1 in range(1, 10):
    print()
    for num2 in range(1, 10):
        if len(str(num1*num2)) == 1:
            print(" "+str(num1*num2),end = ' ')
        else:
            print(str(num1*num2),end = ' ')

Output:

 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 
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to print("\n").

# Start a for loop from 1 to 10.

for num1 in range(1, 10):

# Start a nested loop from 1 to 10.

    for num2 in range(1, 10):

    # Display the multiplication for each value of num.

        print('%4d'%(num1*num2), end = '')

    # Display new line after inner for loop finished.

    print("\n") # change made here

1 Comment

this makes unwanted empty line, you should've used "\r" instead of "\n"
1

As an extra challenge, see if you can get the indention to look correct.

I believe the following both reproduces the example table the most accurately, with no extra leading or internal spaces, and is the simplest implementation compared to other answers:

for row in range(1, 10):
    for col in range(1, 10):
            print(('{:1}' if col == 1 else '{:2}').format(row * col), end=' ')
    print()

OUTPUT

> python3 test.py
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 
>

1 Comment

Thank you for the response.
0

The following code adds a new-line during the first loop after the second has printed the line. This should match what you need.

# Start a for loop from 1 to 10.

for num1 in range(1, 10):

# Start a nested loop from 1 to 10.

    for num2 in range(1, 10):

    # Display the multiplication for each value of num.

      print('%4d'%(num1*num2), end = '')
    print ('\n')
# Display new line after inner for loop finished.

Comments

0
for num1 in range(1, 10):
    for num2 in range(1, 10):
        print('%4d'%(num1*num2), end = '')
    print('\r') # this is what I added to your code

I added line that prints new line when second loop runs out of elements to print

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.