Text input and output¶
If you’re using a Chrome or Edge web browser, you can access a serial console here in the editor. A serial console allows you to print information from your micro:bit on your computer screen, and use your computer’s keyboard to input information into your Python programs.
Open the serial console
Plug your micro:bit into your computer’s USB socket, click on ‘connect’ then pair your micro:bit. When you flash a Python program to your micro:bit, you can access the serial console by clicking ‘Show serial’.
Print output to the serial console¶
You can output text or numbers to the serial console using the print() function:
while True:
print(display.read_light_level())
sleep(1000)
This example prints the current light level once a second.
This can be really useful when debugging a program, or if you want to discover what ranges of sensor readings you get in a particular situation.
Keyboard input¶
You can interact with Python micro:bit programs by entering text on your computer keyboard:
name = input('What is your name? ')
print('Hello', name)
This example asks your name. When you click in the serial console, type your name and press enter, it will greet you.
REPL¶
You can use the serial console to type and execute Python commands directly on your micro:bit.
Exercises¶
- Try to use the mathematical operations to divide 100 by 7 and then round off using floor, but instead of showing to the LED screen, print it on the serial console.
- Get a number from the user between 1 and 100 and print out all the factors of that number.