Course 4.1Lesson 5 of 5
In this project we print a book of multiplication tables and some patterns. We start from the times table of one number, then a loop around it prints the tables of many numbers, each with a title. We plan the output on paper first, then build it in two steps, test it against the plan, and extend it with a neat grid, a table with a header, and a pyramid. The project uses range(), loops that build text, and loops inside loops.
Try the code and the simulations beside the lesson.
After this lesson you can
We want a times table book. For the numbers 2, 3 and 4 it should print
and so on, with a blank line between the tables. We also want to draw a pyramid at the end.
Start with the smallest part: the table of one number. If this works, we can repeat it.
Now wrap that loop in another one, which goes through the numbers.
This way of building, one small part first, and then a loop around it, is a good habit. It works for most programs with loops.
Patterns use the same ideas.
Check one row by hand before you trust the loop: for height 4, row 3 has 1 space and 5 stars.
Write down, on paper, the first and the last line that you expect. For the book of the tables of 2, 3, 4: the first line is Table of 2, the last line with a number is 4 x 5 = 20. Run the program and compare. A mistake of one in the range() stop shows at the last line.
Pick one of these, and do it without looking at a solution.
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
range(1, 11)?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.