Lesson 7: Times tables
About this lesson
This is the seventh in a series of lessons to transition from visual coding to text-based coding with a general-purpose programming language.
Year band: 5-6, 7-8
Curriculum Links AssessmentCurriculum Links
Links with Digital Technologies Curriculum Area
Strand | Year | Content Description |
---|---|---|
Processes and Production Skills | 5-6 |
Design algorithms involving multiple alternatives (branching) and iteration (AC9TDI6P02). |
7-8 |
Design algorithms involving nested control structures and represent them using flowcharts and pseudocode (AC9TDI8P05). Trace algorithms to predict output for a given input and to identify errors (AC9TDI8P06). |
Assessment
Students undertake a self-reflection of the programming task. The teacher can use the completed self-assessments to assist in summative assessment.
In assessing code in languages like Python or JavaScript, consider a rubric that brings in important skills for general-purpose programming.
Learning hook
Let’s talk about strategies to win a game like noughts-and-crosses (tic-tac-toe), or to solve a puzzle like Sudoku. What is your strategy?
- Do you take risks and make guesses?
- Do you write down possibilities, trying to predict the future before making your move? If so, how many moves ahead can you predict?
In 1996, a supercomputer named Deep Blue won a chess game against Garry Kasparov, the reigning chess champion at the time.
As a class, discuss the question: Was Deep Blue smarter than Kasparov?
Some prompts to help in the discussion:
- What sort of strategies might Deep Blue have used?
- Thinking fast may not be the same as thinking smart.
- With noughts-and-crosses, there’s a small number of ways the game can play out, enough for a human to know for certain the best move at any time. What makes chess different to noughts-and-crosses?
- Computers, robots and other machines are great at repetitive tasks that bore or exhaust humans, known as iteration.
Deep Blue used a brute force method to win chess. It used its powerful processor to test out millions of possible futures in the game, over and over, then to select the move most likely to result in a win. Many times, computer programs seem ‘smart’, but they just have access to more data than a human and can repeat a behaviour more quickly.
Long Zheng/flickr, CC BY-SA 2.0
Could a powerful enough computer predict every single possible outcome of a chess game, guaranteeing that it would know how to win at every stage? This video on ‘Shannon’s number’ reveals there are more games of chess than you might ever imagine.
What other approaches are computer scientists taking to make computers smarter at chess?
Learning map and outcomes
In this lesson, students will:
- access an online programming environment for visual code (Scratch) and for general-purpose programming (Python or JavaScript)
- practise writing loops (iteration) in two forms – a while loop and a for loop
- code a times table generator that produces a times table for any number that is entered.
Learning input
Begin by watching the following video introducing loops:
Two types of loops
The video on loops introduces two types of loops: while and for.
A while/ loop is like an if-then structure, except the code inside repeats as long as the condition is met.
Examples:
- repeat while the user has not entered the correct password
- repeat while the enemy is still alive
- repeat while we have time left to win
- repeat while a number hasn’t reached a target (this one is normally done with a for loop)
The closest Scratch equivalent is the repeat until block, but its logic is opposite. It repeats until a condition is met, not while a condition is met.
A for loop is specialised for counting. The code inside repeats a certain number of times.
The counter (also called the index variable) changes each time the loop runs. It may be displayed inside the loop, or it may never be displayed.
Examples:
- repeat a message 10 times
- produce a series of numbers from 1 to 10
- produce a countdown from 10 to 1
- access each item in an array
The closest Scratch equivalent is the repeat n block, but it is less flexible and does not provide access to the counter.
BEGIN For n from 1 to 5 Display ‘DEFCON ’, n End For END PROGRAM OUTPUT: DEFCON 1 DEFCON 2 DEFCON 3 DEFCON 4 DEFCON 5
BEGIN Repeat 4 times Display ‘May it rain,’ End Repeat Display ‘Whisper words of water, may it rain.’ END PROGRAM OUTPUT: May it rain, May it rain, May it rain, May it rain, Whisper words of water, may it rain.
Now implement the two programs in Python or JavaScript, using the for loop techniques learned in the video.
Solution Code
Learning construction
For more on setting up and choosing a language, see Setting Up.
Step 1: Solution development
This video demonstrates coding the solution in Python and JavaScript. Try it yourself before checking the solution code.
By now, you have probably noticed that some coding ‘rules’ seem to matter more than others. Here are some examples of rules versus conventions:
Rule (Do this or you will likely cause a syntax error or a bug in your program!) |
Convention (Do this for code readability, but some programmers may disagree.) |
Use round brackets with display commands. For example: Python: print("Hello") JavaScript: document.write("Hello"); |
Use single quotes inside display commands. Double quotes also work. For example: Python: print("Hello") JavaScript: document.write("Hello"); |
In Python, indent code inside structures. For example if (10 > 5): print('Of course!') |
In JavaScript, indenting is strongly recommended. Indented: if (10 > 5) { document.write('Of course!'); } Not indented: if (10 > 5) { document.write('Of course!'); } |
In JavaScript, use var (or let) to declare variables. (There is a subtle difference between var and let, important once you make complex programs. You can learn more here.) |
Use ‘camel case’ for variable names. For example: myName, orderTotal, noOfStars Some Python coders prefer underscores: my_name, order_total, no_of_stars |
Use == for comparison of two numbers, never a single = sign. For example: Python: if (rating == 5): print('5 stars!') JavaScript: if (rating == 5) { document.write('5 stars!'); } |
Include spaces in Maths statements.
Often, they’ll work without spaces. For example: Python: if ( rating==5): print('5 stars!') JavaScript: if (rating==5) { document.write('5 stars!'); } |
As with spoken languages, some rules used to be universally accepted as strict, but now are sometimes ignored, such as using semicolons (;) at the end of code lines in JavaScript. Whether this will cause a syntax error may depend on the coding environment being used.
Finally, some conventions are just habits. For example, programmers often use the name i for the index variable in loops, because ‘i’ is short for ‘index’. But you could use another letter like n, or a word like index for the variable name.
Solution Code
Step 2: Tinker task
Modify your times table program to go all the way up to 20.
Next, add a second prompt to ask the user how high the times table should go. For example, if they enter 25, the times tables goes all the way up to 25.
Lastly, add a second table that shows the powers for the number. Here’s an example of the output:
5 to the power of 0 = 1
5 to the power of 1 = 5
5 to the power of 2 = 25
5 to the power of 3 = 125
...
You may need to research how to perform this operation in Python or JavaScript. Refer to the Resources section below for cheat sheets.
Solution Code
Challenge
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
Challenge 1
Carefully examine the password check system in the flow chart below.
Image: Flow chart for password check system.
a. Convert the algorithm to pseudocode (structured English) first. You will need to use a while loop to handle the repetition, so it won’t be the exact same sequence as the flowchart.
BEGIN password ← “x1yG2k4!” Display “Enter the password” guess ← input from user While guess is not equal to password repeat Display “Try again” guess ← input from user End While Display “Access Granted!” END
b. Code the program in Python or JavaScript.
Solution Code
Challenge 2
Improve the password check program in Challenge 1 so that there is a limited number of guesses before the program ends. HINT: You’ll need to add a variable to keep track of the number of tries.
Challenge 3 (Optional)
Create a countdown, rather than counting up. The output of the program should be:
Beginning countdown 10 9 8 7 6 5 4 3 2 1 Blast off!
There are at least two ways you can do this, as shown in these solution codes. One way does not require any new knowledge about loops.
Challenge 4 (Optional)
Choose from the links below to find pre-made code with some arrays declared. The code also includes a simple program to display all the items in the colours array using a loop.
Add some code to display all the items in the months array using a loop.
Solution Code
Finally, add some code to find the sum of all the items in the numbers array.
Resources
- Setting up online environments
- Online environments for coding in each language
- Cheat sheets listing basic commands for coding:
- Python Cheatsheet (from Grok Learning)
- JavaScript CheatSheet (Tip: Press the little blue tabs to move Variables, Basics, Strings and Data Types to the top.)