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.
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 write | You get | How many |
|---|---|---|
| range(1, 11) | 1 2 3 4 5 6 7 8 9 10 | ten |
| range(10) | 0 1 2 3 4 5 6 7 8 9 | ten, but starting at nought |
| range(1, 10) | 1 2 3 4 5 6 7 8 9 | nine — the ten times row is missing |
| range(11) | 0 1 2 3 4 5 6 7 8 9 10 | eleven, 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.
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.number * i — which is the whole reason a times table can be written in two lines instead of ten.What you are building
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.