You are going to build a number-guessing game on the micro:bit, then flip the very same project to its Python view and read your own code. The micro:bit picks a secret number from 1 to 9. You press button A to change your guess (it shows on the LED screen) and button B to check it: a happy face means you got it, an arrow points the way to go.
Four moves make the whole game:
The big idea: a variable and a comparison are the same whether you read them as blocks or as Python. Only the way they are written on the screen changes.
Keep this to four minutes: the four moves are the plan, not the teaching. The build is where the time goes.
Open the MakeCode micro:bit editor and build the game in blocks. Use a variable for the secret number and another for the guess, an on button pressed event for A and for B, and an if / else if / else to compare them. Build it a piece at a time and run it as you go. Your finished program looks like this:
let secret = randint(1, 9)
let guess = 1
basic.showNumber(guess)
input.onButtonPressed(Button.A, function () {
guess += 1
if (guess > 9) {
guess = 1
}
basic.showNumber(guess)
})
input.onButtonPressed(Button.B, function () {
if (guess == secret) {
basic.showIcon(IconNames.Happy)
} else if (guess < secret) {
basic.showArrow(ArrowNames.North)
} else {
basic.showArrow(ArrowNames.South)
}
})
Before anyone presses B for the first time: ask what they think will happen if the guess is lower than the secret. Take the prediction before the run. Students who expect a happy face have not yet noticed the comparison has three outcomes, and that is the moment the if / else if / else earns its place.
The wrap back to 1 after 9 is the piece most pairs leave out. Their guess climbs past 9 and the LED shows nothing recognisable.
Run your program on the simulator and play a full round. Press A a few times to count your guess up the LED screen, then press B to check. Here the guess is lower than the secret, so the micro:bit shows an up arrow to say “guess higher”.
Insist on a full round rather than one press. A game that only ever shows the up arrow usually has the comparison the wrong way round, and playing it through is what surfaces that.
Now flip the same project to its Python view (use the toggle at the top of the editor) and read your own code. It is the exact same game, only the way it is written has changed:
secret = randint(1, 9)
guess = 1
basic.show_number(guess)
def on_button_pressed_a():
global guess
guess += 1
if guess > 9:
guess = 1
basic.show_number(guess)
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_b():
if guess == secret:
basic.show_icon(IconNames.HAPPY)
elif guess < secret:
basic.show_arrow(ArrowNames.NORTH)
else:
basic.show_arrow(ArrowNames.SOUTH)
input.on_button_pressed(Button.B, on_button_pressed_b)Find the pieces you placed as blocks: the secret and guess variables, the two button handlers, and the if / elif / else that compares the guess with the secret. The logic is already yours; the only new thing is the typing.
Have them point at the line that matches a block they placed, rather than reading top to bottom. Matching block to line is the skill; reading Python fluently is not today's objective.
Your game is one project you can read two ways. The blocks and the Python describe the same logic: a secret number, a guess you change, and a comparison that decides what the LED shows. Where the blocks nested one inside another, Python uses indentation to do the same job.
Indentation is the one genuinely new idea. Name it here, briefly, because the standalone Python lessons rely on it.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.