Microbit
Intermediate
40 mins
Teacher/Student led
+145 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: What We're Building ~4 mins

    Illustration for Start: what we're buildingToday you build a number-guessing game on the micro:bit. The micro:bit picks a secret number from 1 to 9 and keeps it hidden. You press button A to change your guess, and button B to check it: a happy face means you got it, an arrow tells you to guess higher or lower.

    Then you flip the very same project to its Python view and read your own code.

    2 - Create a New Micro:bit Project ~3 mins

    MakeCode is the editor you write micro:bit programs in. You drag coloured blocks together and it turns them into a real program.

    Go to makecode.microbit.org and click New Project. Call it Number Guesser and click Create.

    You should see an empty workspace with two blocks already in it: on start and forever. You will use on start. Leave forever where it is. The blocks are grouped by colour down the left: Basic blue, Input pink, Variables red, Math purple, Logic teal.

    3 - Make the Secret Number ~3 mins

    In the Variables category (red), click Make a Variable, call it secret and click OK. Drag set secret to 0 into the on start block.

    Now from Math (purple), drag pick random 0 to 10 into the slot where the 0 is, and change the numbers to 1 and 9.

    Read it and predict what the micro:bit will show before you press Run.

    let secret = randint(1, 9)
    jc_microbit_number_guessing s1 blocks

    4 - Show Your Guess on the Screen ~3 mins

    Make a second variable called guess. Drag set guess to 0 into on start, underneath the secret line, and change the 0 to 1.

    Then from Basic (blue), drag show number under it and drop the guess variable into its slot. Press Run. The screen should show 1.

    let secret = randint(1, 9)
    let guess = 1
    basic.showNumber(guess)
    jc_microbit_number_guessing s2 blocks

    5 - Button a Changes the Guess ~3 mins

    From Input (pink), drag on button A pressed onto an empty part of the workspace. Do not put it inside on start.

    Inside it, put change guess by 1 from Variables, then show number guess from Basic. Press A a few times in the simulator and watch the number climb.

    let secret = randint(1, 9)
    let guess = 1
    basic.showNumber(guess)
    input.onButtonPressed(Button.A, function () {
        guess += 1
        basic.showNumber(guess)
    })
    jc_microbit_number_guessing s3 blocks
    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