Exercise: Multiplication Tables

Exercise 9 Very easy

Prerequisites for the exercise

  1. Python Number Basics
  2. All previous chapters

Objective

Create a multiplication table up to 10 for a randomly-chosen positive integer.

Description

Let's go random!

In this exercise, you have to randomly come up with a positive integer not above 10, and then create a multiplication table for that integer upto 12.

For instance, if the integer is 5, you'll compute 5 x 1, 5 x 2, ..., all the way upto 5 x 12.

For the output, start with the following line:

The integer is: <integer>

where <integer> is the randomly-chosen integer.

Next up, comes the multiplication table. Leave a blank line before printing the multiplication table.

Each row of the table is to be presented on a new line in the following form:

<integer> x <i> = <product_i>

where <integer> is the randomly-chosen integer, <i> is the multiplier (starting at 1, and incrementing for each new row) and <product_i> is the product of these two numbers.

Shown below is an example to clarify all these outputs:

The integer is: 4 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40 4 x 11 = 44 4 x 12 = 48
View Solution

New file

Inside the directory you created for this course on Python, create a new folder called Exercise-9-Multiplication-Tables and put the .py solution files for this exercise within it.

Solution

We'll start by computing a random integer n and then output it.

The random integer has to be positive (above 0) and not above 10. That is, it should be in the range 1-10. To generate it, we'll call randint() from the random module.

So, to start with, let's import the module and lay out the other necessary statements:

import random

n = random.randint(1, 10)
print('The integer is:', n)

The next step is to print a blank line, so let's also get this box checked:

import random

n = random.randint(1, 10)
print('The integer is:', n)

print()

With this done, now we only have to print the multiplication table for the random integer. One way is to copy paste 12 lines of print(), but as you'd agree, that's very repetitive, and totally against the DRY principle.

The best option is to use a loop to repetitively execute echo.

Which loop? for or while?

Well, for is useful to iterate a specific number of times or go over a sequence, whereas while is more suited to some condition-based iteration.

Definitely, we'd go with for since we have to repeat a known number of times — 12 specifically.

The general form of each row in the multiplication table is given in the exercise's description above, and based on that we'll construct the call to print() (inside the loop).

import random

n = random.randint(1, 10)
print('The integer is:', n)

print()

for i in range(12):
   print(n, 'x', i + 1, '=', n * (i + 1))
Take note of the expression i + 1 in the print() statement. It's necessary because the for loop begins at 0, which means that in the first iteration i is 0. Without adding 1 to i, we'd be starting off with 0 as the first multiplier, however we ought to start at 1.

This completes our exercise.

Improving the code

Although the code above runs perfectly, there is one very slight change that we could make to it. Let's see whether you could forgive it out.

It has got to do something with i + 1.

Recall, from the section on for in the Python Control Flow chapter, that the range() function can be configured to start at some other integer, instead of 0.

Here, we need to start at 1, so we pass 1 as the first argument to range(). Then to account for this change in the starting point for range(), we need to change the ending point as well.

The starting point changed by 1, so the ending point would also be changed by 1, yielding 13. And because i now begins at 1 in the loop, there's no need to use i + 1 — just replace it with an i.

Altogether our code becomes as follows:

import random

n = random.randint(1, 10)
print('The integer is:', n)

print()

for i in range(1, 13):
   print(n, 'x', i, '=', n * i)

Doesn't this look much better?