# Visual to text codingLesson 8: Guess the number

Years 5-6; 7-8

## Learning hook

The Rubik's Cube was invented in 1974, and is still a popular puzzle today.

There are a number of solutions to the Rubik’s Cube, each involving a sequence of steps. In the Rubik's Cube video, the Lego robot solves the cube using one of these sequences, with a colour sensor to monitor the colours while working. However, the Rubik's Cube can even be solved blindfolded! By repeating a sequence of steps enough times, the solution can be reached even without looking at the colours.

A sequence of steps to solve a problem is referred to as an algorithm.

Invite students to play this game in pairs:

1. Think of a number between 1 and 20. Don’t tell your partner.
3. If they get it wrong, tell them to go ‘lower’ or ‘higher’ and try again. If they get it right, the game is over.

• What’s the quickest way to win this game as the guesser? What number would you start with?
• What would be your next guess if you were told to go ‘higher’, or ‘lower’?

Students can write this game out as a simple algorithm (flowchart or pseudocode is not necessary).

The solution is an algorithm called a binary search. For an example of a binary search, view The famous phonebook video clip.

## 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. code a higher/lower game, where the player must guess a secret number between 1 and 20
3. tinker with the game to make it more challenging.

## Learning input

Begin by watching the video demonstrating the higher/lower game, which uses loops and variables:

Now, examine the Pseudocode below carefully, then answer the questions below it:
```BEGIN
answer ← Input ‘How many months are in a year?’
End Repeat
Display ‘You got it!’
END
```

QUESTIONS:

1. What question does the program ask the user?
2. How many times will the question be asked?
3. What happens once the loop ends?

Here is a more complex version of the program. Read it carefully, then answer the questions.

```BEGIN
tries ← 0
Repeat While answer != 12 And tries < 3
answer ← Input ‘How many months are in a year?’
tries ← tries + 1
End Repeat
Display ‘You got it!’
Else
Display ‘You ran out of tries.’
End If
END
```

QUESTIONS:

1. What is the purpose of the new variable 'tries'?
2. In this version the loop may end even if the user doesn’t get the right answer. Why?
3. Why is the IF-THEN-ELSE structure needed at the end of the program?

## Learning construction

For more on setting up and choosing a language, see Setting Up.

### Step 1: Solution development

The video 'Guess the number' demonstrates coding the solution in Scratch, Python and JavaScript. Try it yourself before checking the solution code.

#### Solution code:

First, change the range for the secret number from 1–20 to a higher number; for example 1–50.

Next, modify the game so that the player only gets a maximum of guesses; for example 5 - otherwise they lose. The higher/lower game already records the number of guesses the player makes.

## 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. Design and code a program to provide a cumulative total as prices are entered, until the user chooses to stop. See the sample output below:

```Welcome! Enter your first price: 17
The total so far is \$17.
Enter another price, or enter STOP to finish: 5
The total so far is \$22.
Enter another price, or enter STOP to finish: 12
The total so far is \$34.
Enter another price, or enter STOP to finish: STOP
Thank you for using this program.
```

a. Prepare pseudocode first.

b. Code the program in Python or JavaScript.

#### Solution code:

2. Code a vowel replacer program by following the video below.

#### Solution code:

3. (OPTIONAL) Ask your students to write a higher/lower game where the roles are reversed. The human player thinks of a secret number, and the computer must guess the answer.

Here are some prompt questions to help students develop their pseudocode:

• What input and output will be required? How will the user tell the computer to go higher or lower? (This is a useful program to design as a class, especially for a device like the BBC micro:bit with limited inputs available to the user.)
• How will the computer reach the correct number? See our discussion of binary search at the beginning of this lesson. Note, after your calculation you will need a way to round down to the nearest whole number. To do this, you can use the int() command in Python and the parseInt() command in JavaScript.
• What things will the computer need to remember as it works toward the correct number? These will need to become variables in the program.

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