Create a multiplication table up to
10 for a randomly-chosen positive integer.
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
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:
<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> 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:
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.
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
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
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))
i + 1in the
print()statement. It's necessary because the
forloop begins at
0, which means that in the first iteration
0. Without adding
i, we'd be starting off with
0as the first multiplier, however we ought to start at
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
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
13. And because
i now begins at
1 in the loop, there's no need to use
i + 1 — just replace it with an
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?