In this activity, you will put the micro:bit into REPL mode and make it evaluate MicroPython statements that you type into the terminal pane.
Looking at your editor’s terminal pane, you can tell if it’s in REPL mode by looking for the REPL prompt symbol >>>. This prompt shows you where to type a MicroPython statement, just to the right of the symbol. Then, when you press Enter the micro:bit will evaluate the statement and you’ll be able to see the result immediately in the same terminal. No need to flash a script!
In the example below, print("Hello Terminal!") was typed at the REPL prompt. After pressing the Enter key, the micro:bit evaluated it, and printed "Hello Terminal!" on the line below. The online editor is shown on the left, Mu on the right.
For either the online editor or Mu:
IMPORTANT: If you are using the python.microbit.org online editor:
The python.microbit.org online editor and Mu editor terminal panes look different, and the steps to enter REPL mode also differ.
In the online editor, you need to start a WebUSB connection before opening the terminal. You will only have to do this once per session, and also every time you disconnect and reconnect the USB cable.
The online editor’s terminal opens into “Serial” mode. In this mode, the terminal will only display messages from a script that’s running. When the terminal is open, switching to REPL is easy. Either click CTRL + D for REPL button, or click the terminal pane and press CTRL + C on your keyboard.
TIP: After clicking Open Serial, the label on the button will change to Close Serial. When you click Close Serial, the terminal pane will disappear, and you will be able to work with code in the editor pane again. Close Serial does not disconnect WebUSB.
In Mu, the REPL button opens a terminal pane and puts the micro:bit into REPL mode.
Now that you’ve got REPL open, it’s time to type some MicroPython statements and see the results.
As of this writing, the micro:bit module hosts MicroPython v.1.9.2. For more information on MicroPython and the REPL feature, please see the information located here: https://docs.micropython.org/en/v1.9.2/esp8266/reference/repl.html [3]
The print function is similar to some of the display methods you experimented with in Writing micro:bit Programs [4]. The main difference is that instead of outputting characters or images to the micro:bit module’s LED display, the print function transmits characters to a terminal.
The print function can display a variety of objects as text in a terminal, and it can also accept more than one object separated by commas. For example, if a variable named n stores the value 5, you can write a statement like print("n = ", n), and it will display n = 5.
Tip: The print function is built into the python interpreter, so your sketches won’t have to include any modules to use it.
You might have noticed that another REPL prompt appeared below the result of your last print statement. Let’s try making REPL print something different.
REPL is not limited to single lines. For example, if you type from microbit import * at the REPL prompt, the MicroPython interpreter in the micro:bit will import the micro:bit module. Then, you can type statements that use MicroPython functions and methods. The next example makes use of the microbit module’s sleep function.
You can also type compound statements into REPL. Compound statements are ones where one line has a colon and the lines below it are indented. Some examples include if… statements and for… loops. This next example also has a for… loop.
Links
[1] https://python.microbit.org/v/2
[2] https://microbit.org/guide/firmware/
[3] https://docs.micropython.org/en/v1.9.2/esp8266/reference/repl.html
[4] https://learn.parallax.com/tutorials/robot/cyberbot/writing-microbit-programs