Paul's Python CoursePPC

Project 6 of 13, for Aaesha

Times Table Trainer

Any times table from 1 to 12, lined up neatly, then a quiz on the hard end, for Aaesha's memory experiment.

About 60 minutes, in 18 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • AaeshaNobody works out seven eights, you just remember it, because times tables are stored facts.
  • AaeshaBut the big ones come back slower and wrong more often, which is called the problem-size effect.
  • AaeshaI'd like a trainer for my volunteers, with the table written out neatly and then the hard end as a quiz.
  • PaulTwelve lines that look the same is exactly the kind of thing loops are for.

Ask which table to practise. Print it from 1 to 12, one line each, with the columns lined up: the first number takes 2 characters and the answer takes 3.

Then print Quiz time. and ask the five hard ones, 8 to 12, in order. After each answer print Right., or No, 63. with the real answer. Last of all, print the score out of 5.

What it prints
Which table? 7
 1 x 7 =   7
 2 x 7 =  14
 3 x 7 =  21
 4 x 7 =  28
 5 x 7 =  35
 6 x 7 =  42
 7 x 7 =  49
 8 x 7 =  56
 9 x 7 =  63
10 x 7 =  70
11 x 7 =  77
12 x 7 =  84
Quiz time.
8 x 7? 56
Right.
9 x 7? 62
No, 63.
10 x 7? 70
Right.
11 x 7? 77
Right.
12 x 7? 84
Right.
Score: 4 out of 5

What you learn

The lesson, step by step

  1. Doing it again

    A loop repeats lines. for tick in range(3): means "do this three times". The lines under it, indented four spaces, are the loop's body, and they run once on every pass. (The name tick gets a step of its own soon.)

    The first line back at the left edge isn't part of the loop. It runs once, after the last pass. So the program on the right prints Tick three times, then Still 3:14 once.

    Like if, the for line ends with a colon, and the indent shows what belongs to it.

    The usual mistake is expecting the line after the loop to repeat as well. Only the indented lines repeat.

  2. What's inside the loop

    A quiz of three questions, then one line at the left edge. Work out which lines repeat.

  3. Which numbers range gives

    range() hands a for loop its numbers, one for each pass.

    • range(4) gives 0, 1, 2, 3. It starts at 0 and stops before 4: four numbers, but not 4 itself.
    • range(1, 5) starts at 1 and stops before 5: 1, 2, 3, 4.
    • A third number is the step, how far to move each time. range(1, 10, 2) gives 1, 3, 5, 7, 9, and range(5, 0, -1) counts down 5, 4, 3, 2, 1.

    The usual mistake is expecting the stop number to be included. It never is. To finish on 12, stop at 13: range(1, 13).

  4. Once round the loop

    • PaulKeep an eye on the dots and count how many passes it makes.

    Watch this loop run, one pass at a time:

    for i in range(4):
        print(i)
    

    Press Start the loop, then Next pass for each pass after that. One more dot lights up every time round, and i holds the next number from the range: 0, then 1, then 2, then 3.

    Four passes, and the first is numbered 0. When the numbers run out, the loop is over.

  5. The number on each pass

    The name after for is the loop variable. On each pass, Python puts the next number from the range into it, and the body can use it like any other variable: print it, or multiply with it. So n * 3 in the body gives a different answer on every pass. Name it for what each number is, like night or question.

    Changing the loop variable inside the body doesn't change how many passes there are. The next pass just puts in the next number.

    After the loop, it keeps the last number it was given, so the last line on the right prints Last night was 3. The usual mistake is thinking it disappears when the loop ends.

  6. Where range starts and stops

    The three times table, or the start of it. The loop variable is n, and the body multiplies with it. Each task changes the range.

  7. Following a loop on paper

    To trace a loop, you follow it the way Python does, one pass at a time, and write down each variable as it changes: one row per pass.

    n   total
    1   1
    2   3
    3   6
    4   10
    

    total = total + n takes what total already holds, adds n, and keeps the answer. So total builds up pass by pass, from the 0 it started at. A quiz score works the same way.

    A print() inside the loop, like line 4 of the example, shows the same table on screen.

    The usual mistake is thinking total ends up holding only the last number added. It holds them all, added up.

  8. Keeping count

    A volunteer answers 1 question on the first day, then 2, then 3. Trace answered pass by pass, on paper if it helps, before you pick.

  9. Lining numbers up

    In an f-string, a colon and a number after the value, like {n:3}, sets a width: the least number of characters the value takes up. Spaces fill the rest, in front, so numbers line up by their last digit.

    On the right, the square brackets show where each value starts and ends. 7 gets two spaces in front, 42 gets one, and 144 fills the width exactly.

    That's how a times table gets straight columns. Give every number the same width, and the x and = signs line up under each other.

    The width is a minimum, not a limit. The usual surprise: a number that's too wide, like 1225, isn't cut short. It takes the room it needs.

  10. Lining it up

    Aaesha wants the table neat for her volunteers, with every x and every = in a column. A messy screen slows people down, and that would muddy her results.

  11. A loop inside a loop

    A loop's body can hold another loop. For every pass of the outer loop, the inner loop runs all the way through. Three rows of three make nine passes of the inner loop in all.

    Each line's indent says which loop it belongs to. On the right, print(row * col, end=" ") is in the inner loop, so it runs for every number. The bare print() is only in the outer loop, so it runs once per row.

    end=" " tells print() to finish with a space instead of starting a new line, so the next number lands on the same line. The bare print() then finishes the line.

    The usual mistake is misreading an indent, and putting a line in the wrong loop.

  12. A loop inside a loop

    Aaesha wants the whole times square on one page, so a volunteer sees every fact at once. That needs a loop inside a loop. Read the indents carefully: each one says which loop a line belongs to.

  13. The whole square

    • AaeshaCould I have the whole square too, please? Every row and column, with all the facts there at once.

    Now the whole square, at any size. For every row, the inner loop goes all the way along the columns, and a bare print() finishes the row.

    Here the inner print() ends with end="": nothing at all, not even a space, because the width of 4 already puts spaces in front of each number.

    Size? 3
       1   2   3
       2   4   6
       3   6   9
    
  14. How Paul would do it

    • PaulI made a chart for my training runs, batches into images, and honestly it's the same shape of job.

    A program like the trainer: Paul's chart for his training runs. His models learn from images a batch at a time. Two loops, one after the other: the first prints the chart, and the second asks three questions, one per pass.

    {batches * size:4} gives the answer 4 characters, because six batches of 256 is 1536 images, and the short numbers should line up under it. score starts at 0 before the questions and goes up by 1 for each right answer.

    The six labels beside the code are Paul's steps. Your file for the Build starts with the same six, as comments.

  15. Fix Riya's version

    • RiyaI made Aaesha a quiz on the nines and tested it on one question, and it got one out of one.
    • RiyaThen Neil got all five right and it said one out of five. Can you have a look?

    Riya's quiz does the nines, 8 to 12. Run it and answer all five right: 72, 81, 90, 99, 108. It should end:

    Score: 5 out of 5
    

    Trace score pass by pass, on paper. Then fix it.

  16. Make the times table trainer

    Write the trainer for Aaesha's experiment: the table from 1 to 12, lined up, then five questions from 8 to 12, and the score.

    Which table? 7
     1 x 7 =   7
     2 x 7 =  14
    ...
    12 x 7 =  84
    Quiz time.
    8 x 7? 56
    Right.
    9 x 7? 62
    No, 63.
    ...
    Score: 4 out of 5
    

    Your file starts with Paul's six steps from the training chart, as comments. Use one for loop for the twelve lines and another for the five questions. In the table, the first number takes 2 characters, {n:2}, and the answer takes 3. Start the score at 0 once, before the questions.

    The checks try other tables, all right and all wrong, and the twelves, where 144 needs all three characters.

  17. Ship it

    • AaeshaFour out of five on the sevens, and the one slip was nine sevens.
    • AaeshaThat's right in the hard end where the theory said it would be, and that's one volunteer done.
    • YahyaI'm doing all twelve tables, and you can put my score up first.

Guides that go with it