We are learning to
- We are learning to decompose a problem and write an algorithm for it in pseudocode.
- We are learning to test an algorithm with a trace table.
Success criteria: I can…
- I can break the guessing game into the steps a computer has to carry out.
- I can write pseudocode that uses a variable, an IF and a WHILE loop correctly.
- I can complete a trace table to show what my algorithm does for given inputs.
- I can find and fix a logic error in someone else's pseudocode.
Lesson sequence
55 min total- 1
Play it, then take it apart
Starter 0–8 minTeacher- Say: “"I am thinking of a whole number from 1 to 100. Guess it. I will only say higher, lower or correct." Use 37 as the secret number.”
- Take guesses one at a time. Tally the number of guesses on the board.
- When someone gets it, say: "Correct. You took that many guesses." Point to the tally.
- Say: “Now imagine I am a computer. A computer cannot just play. It needs every step spelled out. With a partner, list everything I had to do or remember during that game. Two minutes.”
- Collect the parts on the board and tidy them into this list:
- 1.Store a secret number.
- 2.Get a guess from the player.
- 3.Compare the guess with the secret number.
- 4.Tell the player higher, lower or correct.
- 5.Repeat steps 2 to 4 until the guess is correct.
- 6.Count the guesses.
- Say: “Breaking a problem into parts like this is called decomposition. It is the first thing a programmer does.”
- Ask: Which things did I have to remember? (The secret number, the latest guess and the number of guesses. These will be our variables.)
Students- Play the guessing game as a class.
- With a partner, list everything the 'computer' had to do or remember.
- Identify the values that have to be stored.
Whiteboard - 2
Pseudocode for one guess
Main 8–20 minTeacher- Say: “Pseudocode is a plan written halfway between English and code. There is no single official version, so we will agree on our own rules and stick to them.”
- Write the class rules on the board
- Keywords in capitals: SET, INPUT, OUTPUT, IF, THEN, ELSE IF, ELSE, END IF, WHILE, END WHILE.
- One instruction on each line.
- Indent the lines that belong inside an IF or a WHILE.
- Give variables short, clear names.
- Write Version 1 on the board, one line at a time, saying what each line does (see resources).
- Ask: Which lines run for every player, no matter what they guess? (The first three: SET, OUTPUT, INPUT. That is sequence.)
- Ask: The player types 20. Which message appears, and which lines are skipped? (Higher. 20 = 37 is false, 20 < 37 is true, so that branch runs and the ELSE is skipped.)
- Ask: The player types 90? (Lower. Both conditions are false, so the ELSE runs.)
- Ask: The player types 37? (Correct! The first condition is true, so nothing else is checked.)
- Ask: Why do we not need to write ELSE IF guess > secret for the last branch? (If the guess is not equal and not less, greater is the only thing left.)
- Say: “"An IF chooses between branches. That is selection. But this version only allows one guess. What is missing from our list of six parts?" (Parts 5 and 6: repeating and counting.)”
Students- Copy the pseudocode rules and Version 1.
- Predict the output for guesses of 20, 90 and 37.
- Identify what Version 1 cannot do yet.
Version 1 (one guess)SET secret TO 37 OUTPUT "Guess my number from 1 to 100" INPUT guess IF guess = secret THEN OUTPUT "Correct!" ELSE IF guess < secret THEN OUTPUT "Higher" ELSE OUTPUT "Lower" END IF
- 3
Add a loop and trace it
Main 20–32 minTeacher- Say: “To repeat, we use a WHILE loop. It checks a condition. While the condition is true, it runs the indented lines, then goes back and checks again.”
- Ask: In plain English, how long should the game keep asking? (While the guess is not the secret number.)
- Build Version 2 on the board with the class (see resources). Ask for each line before you write it.
- Ask: Why do we SET guess TO 0 before the loop? (The WHILE compares guess with secret straight away, so guess needs a value. 0 can never be the secret number, so the loop is sure to start.)
- Ask: "Why has OUTPUT "Correct!" moved to after END WHILE?" (The loop only ends when the guess is correct, so anything after it runs exactly once, at the right time.)
- Say: “Now we test it without a computer, using a trace table. One row for each pass through the loop.”
- Draw the table headings: Pass, guess, guess ≠ secret?, Output.
- Trace the inputs 50, 25, 37 with the class, filling in each row together:
- Pass 1: guess 50, output Lower, then 50 ≠ 37 is true, so loop again.
- Pass 2: guess 25, output Higher, then 25 ≠ 37 is true, so loop again.
- Pass 3: guess 37, no output inside the loop, then 37 ≠ 37 is false, so the loop ends.
- After the loop: output Correct!
- Ask: What would happen if we had put INPUT guess above the WHILE line and not inside the loop? (The guess would never change, so the condition would stay true and the loop would run forever. That is an infinite loop.)
Students- Suggest each line of Version 2 as the class builds it.
- Explain why guess is set to 0 and why "Correct!" comes after the loop.
- Complete the trace table for the inputs 50, 25 and 37.
Version 2 (keeps asking)SET secret TO 37 SET guess TO 0 OUTPUT "Guess my number from 1 to 100" WHILE guess ≠ secret INPUT guess IF guess < secret THEN OUTPUT "Higher" ELSE IF guess > secret THEN OUTPUT "Lower" END IF END WHILE OUTPUT "Correct!"
Trace table for Version 2 with inputs 50, 25, 37Pass
- 1.guess = 50, output Lower, guess ≠ secret is true Pass
- 2.guess = 25, output Higher, guess ≠ secret is true Pass
- 3.guess = 37, no output, guess ≠ secret is false After the loop: output Correct!
- 4
Your turn: count the guesses and set a limit
Main 32–47 minTeacher- Put students in pairs. Write the two tasks on the board.
- Task A: Change Version 2 so that it counts the guesses and finishes with a message such as: You took 3 guesses.
- Task B: Change your Task A answer so that the player gets at most 7 guesses. If they run out, the game tells them the secret number.
- Say: “Write Task A first. Then trace it with the inputs 80, 30, 37 and check that the final message says 3 guesses. Only then start Task B.”
- Move around the pairs. Ask: "Where does count start, and what value does it start at?" (Before the loop, at 0.)
- Ask: Where does it go up, inside or outside the loop? Why? (Inside, once for every guess.)
- For Task B, ask: "The loop should carry on only if two things are both true. What are they?" (The guess is wrong AND the count is less than 7.)
- Ask: After the loop ends, how does the program know whether the player won or ran out? (It checks with an IF: guess = secret means they won.)
- At 11 minutes, show the model answers (see resources). Say: "Yours may be worded differently and still be right. Check it by tracing, not by matching mine line for line."
- Say: “"Trace Task B with seven wrong guesses of your choice. What is the last line of output?" (Out of guesses. The number was 37.)”
- Tip: The most common slip is putting SET count TO 0 inside the loop, which resets it on every pass. A trace table shows this at once.
Students- Write Task A in pseudocode and trace it with 80, 30, 37.
- Write Task B with a seven-guess limit.
- Check both against the model answers by tracing.
Model answer, Task ASET secret TO 37 SET guess TO 0 SET count TO 0 OUTPUT "Guess my number from 1 to 100" WHILE guess ≠ secret INPUT guess SET count TO count + 1 IF guess < secret THEN OUTPUT "Higher" ELSE IF guess > secret THEN OUTPUT "Lower" END IF END WHILE OUTPUT "Correct! You took ", count, " guesses" Trace with 80, 30, 37: count goes 1, 2, 3. Outputs: Lower, Higher, then Correct! You took 3 guesses.
Model answer, Task BSET secret TO 37 SET guess TO 0 SET count TO 0 OUTPUT "Guess my number from 1 to 100. You have 7 guesses." WHILE guess ≠ secret AND count < 7 INPUT guess SET count TO count + 1 IF guess < secret THEN OUTPUT "Higher" ELSE IF guess > secret THEN OUTPUT "Lower" END IF END WHILE IF guess = secret THEN OUTPUT "Correct! You took ", count, " guesses" ELSE OUTPUT "Out of guesses. The number was ", secret END IF
- 5
Find the bug
Plenary 47–55 minTeacher- Display the faulty pseudocode (see resources). Say: "A student wrote this. It runs, but players complain that the game is impossible. Work alone. Trace it with a first guess of 40 and find the bug."
- Give 3 minutes.
- Ask: What is the output when the guess is 40? (Lower.)
- Ask: What should it be? (Higher, because 40 is less than 62.)
- Ask: So what is the bug, and how do you fix it? (The two messages are swapped. Swap Higher and Lower, or swap the < and > signs.)
- Say: “This is a logic error. The computer will run it without complaint. Only testing with real values shows the problem, which is why we trace.”
- Exit task on paper: "1. Write the corrected IF block. 2. Name one line in the pseudocode that is an example of selection and one that is an example of iteration. 3. Why is 7 a fair limit for a 1 to 100 game? Hint: think about guessing 50 first."
- Collect the exit tasks with the Task A and B pseudocode.
- Go over question 3 if time allows: (Guessing the middle each time halves the numbers left: 100, 50, 25, 12, 6, 3, 1. Six guesses narrow it to one number and the seventh names it, so seven well-chosen guesses are always enough.)
Students- Trace the faulty pseudocode with a guess of 40.
- Identify and fix the logic error.
- Complete the exit task and hand it in.
Faulty pseudocodeSET secret TO 62 SET guess TO 0 WHILE guess ≠ secret INPUT guess IF guess < secret THEN OUTPUT "Lower" ELSE IF guess > secret THEN OUTPUT "Higher" END IF END WHILE OUTPUT "Correct!"
Exit task answers- 1.IF guess < secret THEN OUTPUT "Higher" ELSE IF guess > secret THEN OUTPUT "Lower" END IF.
- 2.Selection: the IF or ELSE IF line. Iteration: the WHILE line.
- 3.Each sensible guess can rule out about half of the numbers left, so six guesses narrow 100 numbers down to one (100, 50, 25, 12, 6, 3, 1) and the seventh guess names it.
Key vocabulary
Differentiation
Support
- Give the pair Version 2 cut into separate line strips, plus two extra strips (SET count TO 0 and SET count TO count + 1). They complete Task A by deciding where the two new strips go and testing each position with a trace.
- Provide a part-filled trace table for 80, 30, 37 with the guess column done, so the student fills in count and output and can see the pattern of one row for each pass.
Extension
- Ask the student to write pseudocode for the reverse game: the player thinks of a number from 1 to 100 and the computer guesses it, always choosing the middle of the range that is left. They need variables for the lowest and highest possible values.
- Ask the student to add input checking to Task B: if the player enters a number below 1 or above 100, the program gives a warning and the guess does not count. They then write three test inputs that prove it works.
Accessibility
- Offer the pseudocode on a printed sheet with wide line spacing and the indentation shown by a vertical line, so students with tracking or reading difficulties can follow the blocks.
- Students can write the symbol ≠ as 'not equal to' or as <>. Accept any consistent form.
- Keep a short glossary of the keywords on the board with a plain meaning beside each (INPUT: get from the player. OUTPUT: show on the screen) for students who are new to English.
- Pairs can talk through a trace aloud, with one student recording, for those who find writing tables slow.
Assessment
- During the guided trace, cold call for the next cell of the trace table and listen for whether students evaluate the WHILE condition after each pass.
- Check each pair's Task A by asking them to show the trace for 80, 30, 37. The count column should read 1, 2, 3 and the final output should report 3 guesses.
- Collect the exit task. Question 1 shows whether the student can fix a logic error, and question 2 whether they can tell selection from iteration.
Resources
- Whiteboard or projector for the pseudocode versions
- Paper or exercise books for pseudocode and trace tables
- The faulty pseudocode, displayed or printed
- Optional: Version 2 printed and cut into line strips for support