Today you are coding in Python on the micro:bit editor. You will build a Rock-Paper-Scissors game step by step: predict what a round should do, then build, run and fix.
You can already use simple Python and make the micro:bit respond. By the end you will have a full game where you press buttons for rock, paper or scissors, the micro:bit picks at random, and the display shows who won.
Before you run anything, think: if you choose rock and the micro:bit chooses scissors, who should win, and what should appear on the screen?
Keep the start short. Devices on the micro:bit Python editor, students logged in. Optional predict: before anyone runs code, ask what should happen if the student picks rock and the micro:bit picks scissors, and what the display should show. Do not take long answers; park one or two ideas to revisit in make sense. Model that work is in pairs if that is your class routine, then move straight into the build.
In this lesson you'll create a Rock-Paper-Scissors game using your Micro:bit and the Micro:bit will act as your opponent. You'll use the buttons to make your choice, generate a random choice for the computer using the random module, compare the outcomes with if-else statements, and display the results on the LED screen with messages like 'You win!'. We'll store the possible choices in a list to practise list handling.
By the end of this lesson, you'll have a fully interactive game. Open the Micro:bit Python editor at python.microbit.org and start a new project.
Frame the studio: they will build Rock-Paper-Scissors with the micro:bit as opponent, buttons for input, random for the computer, and if-else to decide the winner. Confirm everyone has a new project at python.microbit.org. No code on the board yet beyond opening the editor. Watch for students opening the block editor by mistake; steer them to Python.
In this step, we'll import the necessary modules. We need the microbit module for hardware access and the random module to generate the computer's choice.
Start by adding the following complete code to your editor:
from microbit import *
import randomModel the two import lines on the board. Key question: why import random if nothing random happens yet? Common bug: misspelling microbit or random, or missing the asterisk import. Run once to prove a clean run with no output is success. Differentiation: if imports fail, check they are in the online micro:bit Python editor not a desktop-only setup.
Next, we'll create a list to store the possible choices: rock, paper, and scissors. Lists are great for holding multiple related items, and we'll use this to select the computer's choice randomly.
Update your code to the following complete version:
from microbit import *
import random
choices = ["rock", "paper", "scissors"]
display.scroll("Ready")
Model the choices list and the Ready scroll. Stress typing each line. Key question: why store the three options in a list rather than three separate variables? Common bug: curly quotes or wrong brackets around list items; mixed quotes also break the run. After Run, every board should scroll Ready. Support: dictate the list items slowly. Extension: ask what else could go in the list later.
Now, let's handle user input using the buttons. We'll decide: press button A for rock, button B for paper, and both buttons for scissors. We'll use a loop to wait for a button press.
The while True: creates an infinite loop that keeps checking for button presses until one is detected. Inside the loop, we use if-elif statements to check which button(s) are pressed. When a condition is true, we assign the user's choice to a variable and use the break statement. The break statement exits the loop immediately, stopping the continuous checking and allowing the program to continue with the next lines of code. The sleep(100) adds a short delay to prevent the loop from running too quickly and using too much processing power.
Update your code to this complete version:
from microbit import *
import random
choices = ["rock", "paper", "scissors"]
while True:
if button_a.is_pressed() and button_b.is_pressed():
user_choice = "scissors"
break
elif button_a.is_pressed():
user_choice = "rock"
break
elif button_b.is_pressed():
user_choice = "paper"
break
sleep(100)
display.scroll(user_choice)if-else for control.This is the input loop. Model the while True structure and the order of the if-elif tests on the board, with both-buttons first. Key question: what does break do, and what happens if you forget sleep(100)? Common bugs: wrong indent on break; testing A or B before both so scissors never triggers; holding buttons too briefly in the simulator. Differentiation: weaker pairs can get a spoken walkthrough of one button path only before adding the others.
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.