As you’ve now seen, print function calls can be entered at the REPL prompt to display messages and values.
Recall from Using the Serial Monitor that you can flash a script to the micro:bit that prints a message, and open the Serial monitor to see that message. 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.
Example script: print_to_terminal.py
- Connect your micro:bit module to your computer with its USB cable.
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
- Enter, name, and save print_to_terminal.
(See Save & Edit Scripts.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# print_to_terminal.py from microbit import * sleep(1000) print("Hello Terminal!")
- Click Show serial to open the serial monitor.
(See Use the Serial Monitor.) - Verify that the Hello Terminal! message is displayed.
- Want to see it again? Click the 3 dots ⋮ symbol to the right of Show/Hide serial, then click Send CTRL-D to reset.
- Click Hide serial button to close the serial monitor.
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.
Try This: Print Something Different
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.
- Change project's name from print_to_terminal to print_to_terminal_try_this.
- Replace the Hello Terminal! with your own new message.
- Save the modified script.
- Click the Send to micro:bit button.
- Check the results in the serial monitor.
Did You Know?
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.
Example script: display Integer Values
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.
- Enter, name, and save print_value_to_terminal.
- Click the Send to micro:bit button.
# print_value_to_terminal.py from microbit import * sleep(1000) print("Hello Terminal!") value = 10 print("value = ", value)
- Check the results in the serial monitor.
- Verify that the terminal displays Hello Terminal followed by value = 10 on the next line.
Print Values to Terminal in a Loop
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.
Example script: print_looped_terminal_values.py
- Enter, name, and save print_looped_terminal_values.
- Click the Send to micro:bit button.
# 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!")
- If the serial monitor isn't already open, click Show serial.
- Click Send CTRL-D to reset.
- Verify that the terminal displays value = 5, value = 4, ...value = 1, Blastoff!