Lab 7: Loop in a loop

Use Zybooks 5.43 Lab 7: Loop in a loop to write a program that prompts for the number of rows and columns displays a multiplication table, such as the following:

How many rows: 10

How many columns: 10

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100


Note that the columns should all line up, and each number should be right-justified within 4 spaces. To do this you will need a loop inside of another loop. The outer loop should handle each row, and the inner loop should handle the columns within that row. This should give you a structure something like the following:

# Loop for each of 10 rows

# Loop through each column on this row

# Display the row * col value


Helpful hints:

  1. To print a number and leave the cursor on the same line, remember to use: print( number, end = ' ')

  2. Smaller numbers will take up less space than larger numbers. To get the columns to line up, either add some extra print statements to print extra spaces when necessary, or use formatted printing using an f-string (See Zybooks section 3.10 on String Formatting) For example, to print an integer variable number in 2 spaces, you would use: print(f'{number:2}')

  3. Consider developing your code in a replit project that you create, so you can collaboratively edit with your lab partner(s) as you work on it.

  4. If you understood the reading on for loops, then you can substitute while loop code such as:

counter = 1

while counter < 5:

print( counter, end = ' ')

counter += 1


with the equivalent:

for counter in range( 1, 5 + 1): # Have to add 1 to 5, because a for loop stops at limit-1

print( counter, end = ' ')


Your output will have to match the expected output exactly (including spaces) to get credit, so having everything line up is important!

There is no extra credit for today's lab, however if you finish early, create your own version of the program somewhere else (Zybooks 1.24 or in replit) and see if you can figure out how to print just a subset of the multiplication table, from some starting row and column up to some ending row and column.