[Logo for The C Shore Landing Page]
The C Shore

More Mu

Table of Contents

Headless Mu

  • A brief discussion of Mu
    • Tabs vs. Spaces (the indent problem from last module)
    • Load, Run, Stop, Save, and Check

Autocompletion in Mu

You've probably noticed by now that Mu offers you suggestions of what you might be trying to type. If Mu is offering you an choice that is correct, use the cursor (arrow) keys to select that option from the list, and then press Tab.

It is important that you do not press Tab unless the suggest text is correct. If you press Tab on the wrong text, you will need to use the backspace key to erase it.

Try using autocomplete with the code we work on below.


Loop forever in Python

while True:
    print("I will run forever.")

If you have trouble creating this program, in Mu, click ‘Load’ and load the following file from the list of files that comes up:

modules03-03-while-forever.py

Click Run, and watch what happens until you click ‘Stop’.


You may recall seeing something like this in the first module with the LED.

As you've probably guessed, this tells Python to print “I will run forever” for as long as the program runs (which is forever unless you stop it manually).

This takes two things being true:

  1. The use of while True:
  2. The indented print("I will run forever.") — indent means that there are spaces in front of the code.

All Python while statements start with while, have some condition (which is test that when it is False, will stop the loop — since True is never False this loop runs forever), and with a colon (:).

Any group of code immediately after a while statement that is indented the same amount will run as as long as the while condition is True.

In this case we have a single line after the while statement, so it is the only thing that is repeated.

Try adding other print statements after the while.


While loops (counter)

Sometimes while loops are written to repeat a specified number of times:

counter = 0
while counter < 10:
    print("Counting...")
    counter += 1

In this example, the condition is counter < 10. Since we add 1 to counter each time the loop runs, the loop will run 10 times.