Microbit
Intermediate
40 mins
Teacher/Student led
+70 XP

Build a Number-guessing Game on the Micro:bit, Then Read It in Python

Build a number-guessing game on the micro:bit using blocks, with variables for a secret number and your guess. Button A changes the guess on the LED screen and button B checks it, showing a happy face for a correct guess or an arrow pointing the way to go. Then switch the same project to its Python view and read your own code, matching each line back to the block you placed.

Teacher Class Feed

Load previous activity

    1 - Start: a Number-guessing Game ~4 mins

    Illustration for Start: a number-guessing gameYou 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:

    • Pick a secret number from 1 to 9 and store it.
    • Keep a guess, starting at 1, and show it on the LED screen.
    • On button A: add 1 to the guess (wrapping back to 1 after 9) and show it.
    • On button B: compare the guess with the secret. Equal shows a happy face, lower shows an up arrow (guess higher), higher shows a down arrow (guess lower).

    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.

    2 - Build It in Blocks ~14 mins

    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)
        }
    })

    3 - Watch It Run on the Micro:bit ~6 mins

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

    Number-guessing game running on the virtual micro:bit: the guess counts up on the LED, then an arrow shows higher or lower

    4 - Read It in Python ~10 mins

    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.

    5 - Make Sense ~3 mins

    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.

    123learn · Online learning platform

    Unlock the full learning experience

    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.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more