# Visual to text codingLesson 7: Times table

Years 5-6; 7-8

## 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

#### THOUGHT EXTENSION 1:

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.

#### THOUGHT EXTENSION 2:

What other approaches are computer scientists taking to make computers smarter at chess?

## Learning map and outcomes

In this lesson, students will:

1. access an online programming environment for visual code (Scratch) and for general-purpose programming (Python or JavaScript)
2. practise writing loops (iteration) in two forms – a while loop and a for loop
3. 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:

```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.

## 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.

#### Solution code:

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 for cheat sheets.

## 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.

1. Carefully examine the password check system in the flow chart below.

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.

b. Code the program in Python or JavaScript.

#### Solution code:

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.

#### Solution code:

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.

#### Solution code:

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.

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
• Scratch
• repl.it: an online environment suited to Python
• JSFiddle: an online environment suited to JavaScript
• 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.)