Microbit
Advanced
32 mins
Teacher/Student led
+80 XP

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

Teacher Class Feed

Load previous activity

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

    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.

    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 - How the Game Works ~4 mins

    Before you build it, picture the four moves the game makes:

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

    3 - Build It in Blocks ~8 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. 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)
        }
    })
    

    4 - Watch It Run on the Micro:bit ~4 mins

    Run your program on the simulator and play it. 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

    5 - Read It in Python ~6 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.

    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