Keep many values in one list, and pick one by its place, counted from 0.
A playlist, a lunch menu, a class roll: each is a list of things in order, and you pick one by its place.
What you should already know:
Count with a variable· Use a variable to remember a number, and change it each time a button is pressed.
If and else· Use one if … else block to choose between two things, so exactly one of them happens.
A variable holds one value. A list holds many, in order, under one name, such as answers. Each item has a place number (an index), and the micro:bit counts places from 0. You find list blocks in Advanced › Arrays (an array is another name for a list).
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
Show as code
lesson.ts
let answers = ["Yes", "No", "Maybe"]
input.onButtonPressed(Button.A, function () {
basic.showString(answers[1])
})
get value at 1 takes one item out of the list, by its place.
RunRun it and compare the result with your prediction.
Run
Press A and read the word as it scrolls.
Show as code
lesson.ts
let answers = ["Yes", "No", "Maybe"]
input.onButtonPressed(Button.A, function () {
basic.showString(answers[1])
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
InvestigateTrace how each line of code connects to what you see.
Investigate
Show as code
lesson.ts
let answers = ["Yes", "No", "Maybe"]
input.onButtonPressed(Button.A, function () {
basic.showString(answers[1])
})
Fill in the item at each place.
Work out the last place of a longer list.
The list is Yes, No, Maybe. What is at each place?
place
item
0
1
2
A list has 5 items. What is the place number of the last one?
ModifyChange the code on purpose and check the result.
Modify
Add a fourth word, Later. Which place is it at? Make B show it.
A tilt meter: what shows for each value of acceleration x?
acceleration x
shows
600
200
-50
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make
Make a mood chooser. The list of moods and the variable choice are made for you. Each press of A shows the next mood: Happy, then Tired, then Hungry, then back to Happy.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
A list (MakeCode calls it an array, in Advanced › Arrays) holds many values in order under one name. Each item has a place (an index), counted from 0: in Yes, No, Maybe, Yes is at place 0. So a list of 3 has places 0, 1 and 2, and the last place is one less than the number of items. get value at takes out the item at a place. Picking at random is next lesson.
Running the lesson
Predict: most students say Yes. Ask: which place does the micro:bit start counting from? Or: where does a ruler start?
Run: No. Say it plainly: lists count from 0, like a ruler starts at 0.
Investigate: the last-place question is where counting from 0 bites.
Modify: adding Later and asking for place 3 makes students count.
Mix it up: a tilt meter from the Decide unit, read from the top.
Make: the chooser uses a variable as the place, and an if to go back to 0.
The hard part (on purpose)
Counting places from 0. Ask them to point at each item and count aloud from zero.
When students get stuck
The list block has only one or two places. Ask: Where is the + on the list block? Fix: Click + on the list block to add places.
The chooser shows nothing after the third press. Ask: What is choice after three presses, and is there a place with that number? Fix: There is no place 3. When choice reaches 3, set it back to 0.
A number shows instead of the word. Ask: Is it show string or show number? Fix: Words need show string, from Basic.
Stretch and support
Stretch: Make B step backwards through the moods, going round to the last one after the first.
Support: Give students the A event with change and show already in, and ask them only to add the if that goes back to 0.
Where it leads
Next lesson: pick an item at random, however long the list is. Class idea: a list of the week's homework, one item per day.
What each step is for
Investigate: A list keeps items in order, counted from 0, so the first item is at place 0 and the last place is one less than the number of items.
Modify: Every item has a place, and get value at takes out any one of them.
Mix it up: The first true question wins, so the bigger tilt is asked about first.