As you’ve now seen, print function calls can be entered at the REPL prompt to display messages and values. MicroPython scripts can also use print to display messages and values as the scripts run. This can be helpful for displaying longer messages, and also for displaying what’s happening to values at certain points in your script. And, since you flash the script to the micro:bit, your code travels with your device.
In this activity, you will print messages, values, and combinations of both in the terminal.
The Online Editor has to be connected to the micro:bit before it can display text messages. If the second button from the left displays “Disconnect”, it means that the micro:bit is connected. If it displays “Connect”, it means the micro:bit is not connected and you need to connect it.
Now that the online editor is connected, you can run a script that prints a message and open the terminal to see that message. With this connection, you can also flash scripts into the micro:bit by simply clicking the online editor’s Flash button. (No more switching to a file browser and dragging files into the micro:bit drive!)
# print_to_terminal.py from microbit import * sleep(1000) print("Hello Terminal!")
After clicking “Send CTRL-D to reset”, the first message the terminal displays is soft reboot. That’s letting you know that the terminal is doing the equivalent of pressing and releasing the micro:bit module’s Reset button, (which is next to its USB connector). After that, the micro:bit runs the script and whenever it executes a print statement, the result appears in the terminal. In this case, it’s the Hello Terminal! message.
If the script ends by running out of statements, the terminal then displays technical information about the micro:bit, followed by a prompt to type help() for more info, and ends with the REPL prompt. Whenever “CTRL-D to reset” is clicked, the micro:bit will restart the script and display these lines again.
To reset the micro:bit, you can also click the black Terminal pane and press CTRL+D (CMD+D in Mac).
To exit a running script and go straight to the REPL prompt, you can also click the “Send CTRL-C for REPL.”
In Mu, there’s a REPL button that opens a terminal pane and puts the micro:bit into REPL mode. To run a script and see its print statements, simply click the terminal pane and then press CTRL + D. You can also press and release the micro:bit module’s Reset button. It’s the button right next to the micro:bit’s USB socket.
# print_to_terminal.py from microbit import * sleep(1000) print("Hello Terminal!")
You should see Hello Terminal! appear in the REPL terminal. To learn more about the other messages, read the section titled Inside the Terminal Display earlier on this page.
Mum on Mu — This is the last time that the Cybersecurity lessons will refer to the Mu application, however Mu’s REPL is able to handle all of these lessons in the same way that the online editor’s terminal does.
What if you want to display a different message in the online editor? Try modifying the text in between the parentheses and quotation marks after the print command.
The micro:bit’s print function is completely different from the display object and its methods. One of those outputs is not as fast as the other. The display methods are for the 5x5 LED grid, and each call has to display each character long enough for a human to read it. In contrast, the micro:bit print function can send characters very rapidly, with each character taking only about 1/10,000th of a second to transmit.
You might have noticed that print can display values stored by variables, along with strings of characters. That same functionality is also available when you add print calls to scripts.
# print_value_to_terminal.py from microbit import * sleep(1000) print("Hello Terminal!") value = 10 print("value = ", value)
Let’s try a countdown timer in the terminal using a while loop. The while statement repeatedly executes statements that are indented immediately below it. The loop repeats “while” its condition is true. In this program, the while loop repeats while the value variable is greater than or equal to 0.
# print_looped_terminal_values.py from microbit import * sleep(1000) print("Hello Terminal!") value = 5 while value > 0: print("value = ", value) sleep(1000) value = value - 1 print("Blastoff!")
Links
[1] https://python.microbit.org/v/2