Interactive Scripting with REPL

In this activity, you will use the micro:bit in REPL mode, making it evaluate MicroPython statements that you type into the monitor.  

Looking at your editor’s monitor, use 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 pane. No need to flash a script!

In the example below, print("Hi from REPL!") was typed at the REPL prompt.  After pressing the Enter key, the micro:bit evaluated it, and printed Hi from REPL! on the line below.  


 

REPL with the python.microbit.org online editor

  • Use a Google Chrome or Microsoft Edge browser to go to python.microbit.org.
  • Connect your micro:bit to your computer with a USB cable.  For this activity, it can be either the entire cyber:bot, or just the micro:bit on its own.
  • Click Connect, select your micro:bit, and then confirm with the Connect button.

Using REPL mode is easy. Just click the 3 dots to right of Show serial (or Hide serial if the Serial monitor is open) and choose Send Ctrl+C for REPL.  

  • Click CTRL + C to put the micro:bit into REPL mode.

TIP:  After flashing any script to the micro:bit module with the Serial monitor open, you can see a REPL prompt appear at the bottom of the monitor by default. You may also start working in REPL mode from this prompt.  

 

Code Interactively with REPL

Now that you’ve got REPL open, it’s time to type some MicroPython statements and see the results.

  • Click inside the monitor just to the right of the >>> REPL prompt.
  • Type the statement print("Hello Terminal!") and then press Enter.
  • Check to make sure the micro:bit responded with Hello Terminal! on the next line.

 

The Print Function

The print function is similar to some of the display methods you experimented with in Writing micro:bit Programs.  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 the Serial monitor.

The print function can display a variety of objects as text. 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.


Try This: Print something different

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.  

  • Type print(" your custom message ")
  • Press Enter and check the result that appears on the next line.  Does it match the custom message you thought up and entered?


Your Turn: Multiple Statements with REPL

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.

  • At the REPL prompt, type from microbit import * and press Enter.  If a >>> REPL prompt appears on the next line, it means the micro:bit executed the statement.
  • Type for n in range(1, 6): at the next REPL prompt, and press Enter.  The REPL prompt that appears on the next line should be instead of >>>.  This indicates that you are adding a statement that’s an indented part of the compound statement.
  • Type print("n = ", n) at the indented REPL prompt.
  • Another indented REPL prompt should appear.  Type sleep(1000) and press Enter.
  • Press Enter three more times to exit the compound statement and make REPL evaluate the loop.
  • Verify the monitor displays the result, which should be n = 1 followed by n = 2 on the next line a second later.  The monitor should display a new n = value once per second on each new line until it reaches 5.  Then, it should display another >>> REPL prompt.