You can already run short Python programs and change what appears on the screen.
Today you will build a higher or lower game in Python on the micro:bit. You will use variables to store values, take input from the buttons, and show output on the display. You will predict, build, run and fix as you go.
Keep this short. Devices on, Python micro:bit editor open, students logged in. Optional predict before anyone runs code: ask what a variable might store in a higher or lower game and what the buttons might do. Do not take long answers; park one or two ideas to revisit in make sense. Then move straight into the build.
In this lesson, you will learn about variables in Python, how to declare and assign them, their types, and good naming conventions. You will also create a higher or lower game using the Microbits Python editor.
Frame the lesson as a PRIMM code-along. Students will meet variables first, then build the higher or lower game. Stress that they will run and fix as they go. Pair students if that is your usual setup; note pairs in your circulation plan, not on the student screen.
Variables are used to store values in a program. They have a name and can store different types of data such as numbers, text, and more. In Python, you don't need to specify the type of data a variable will store, as it automatically detects the type based on the value you assign to it.
Let's take a look at some examples of declaring and assigning variables of different types:
age = 42
message = "Hello, World!"
isOpen = True
productPrice = 3.14In this example, we have created four variables:
Run this as Predict then Investigate. Ask what kind of value each example name might hold before they look closely. Key question: why does Python not need you to write the type? Watch for the misconception that the name of the variable forces the type.
To declare and assign a variable in Python, you simply write the variable name followed by an equal sign and the value you want to store. For example:
my_variable = 10This creates a variable named 'my_variable' and assigns it the value 10.
Model one clean assignment on the board using a clear name. Key question: which side is the name and which side is the value? Common bug: students write the value first or use a comparison sign instead of assignment. Differentiation: weaker pairs only change the value; stronger pairs add a second variable of their own.
Python has several variable types, including:
Python automatically detects the variable type based on the value you assign to it. For example:
number_variable = 5
float_variable = 5.5
string_variable = 'Hello'
boolean_variable = True
Quick Investigate beat. Ask students to spot integer, float, string and boolean from short examples without copying anything out. Misconception: decimal numbers are integers, or True and False need quotes. Keep the pace brisk so time stays for the game.
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.