JIT — Intro to Data Science · Python Lab · Problem 14 of 30

A multiplication table

Counting when there is no list to count through — and the one-number difference that quietly costs you a row.

Counting when there is nothing to count through

The loop in problem 13 walked through names that already existed. A multiplication table has no list to walk through — you want the numbers 1 to 10, and nobody has written them down anywhere.

range() produces them on demand.

New word — range range(1, 11) hands the loop the numbers 1, 2, 3 … 10. Two numbers: where to start, and where to stop.
The stop value is not included. This is the same rule as the slice in problem 6, and for the same reason: range(1, 11) gives you ten numbers, and 11 − 1 = 10. Subtract the first from the second and you have the count.

Four ranges that look similar and are not

You writeYou getHow many
range(1, 11)1 2 3 4 5 6 7 8 9 10ten
range(10)0 1 2 3 4 5 6 7 8 9ten, but starting at nought
range(1, 10)1 2 3 4 5 6 7 8 9nine — the ten times row is missing
range(11)0 1 2 3 4 5 6 7 8 9 10eleven, including a pointless nought row

Given one number instead of two, range assumes you meant to start at nought. That is exactly right when you are counting positions in a list, and exactly wrong when you are writing out a times table.

A third number: the step range(2, 11, 2) counts 2, 4, 6, 8, 10 — start, stop, and how far to jump each time. Leave the step out and it is 1. Make it negative and the count runs backwards.
The loop variable holds a number now, not a name In problem 13 the box held a name from a list. Here it holds 1, then 2, then 3. It is an ordinary number, so you can do arithmetic with it — number * i — which is the whole reason a times table can be written in two lines instead of ten.

What you are building

Table of 7 7 x 1 = 7 7 x 2 = 14 ... 7 x 10 = 70 Even multiples only: 7 x 2 = 14 7 x 4 = 28 7 x 6 = 42 7 x 8 = 56 7 x 10 = 70 Done
Coming later Both loops here print and forget. Carrying a total forward from one pass to the next — so that the loop ends holding an answer — is problem 15.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below. Five pieces are missing. Two of them are ranges, where a difference of one number changes how many rows you get and which ones.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The number being tabled is yours to change. Count the rows carefully — a table that is one row short still looks perfectly tidy.

Your programme

Output

Nothing has run yet.