MicroPython Loop Tracer
Run the MicroPython Loop Tracer MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim
Loops are how a robot face blinks five times without you typing five lines of
code. This MicroSim walks you through a short for loop and a short while
loop one printed line at a time. Before each line appears, you type what you
think the loop will print, and only then does the tracer reveal the answer.
The goal is simple: you should be able to run a loop in your head, one
iteration at a time, and correctly predict every variable value and every
printed line before the computer shows you.
How to Use
- Pick an example from the Example dropdown: For Loop or While Loop.
- Read the code and decide what the very next printed line will be.
- Type your prediction in the text box, such as
Blink 0orReady 3. - Click Check. The tracer tells you whether you were right, adds the real line to the output log, and updates the variable table.
- Click Next Step to move to the next iteration and predict again.
- Click Reset at any time to start the selected example over from step 1.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Grade Level
9-12 (High School)
Duration
10-15 minutes
Prerequisites
- Know what a variable is and that it holds a value that can change.
- Know that
print()sends one line of text to the serial console. - Understand that indentation marks which lines belong inside a block.
- Be able to read a comparison such as
count > 0as True or False.
Activities
- Exploration (5 min): Run the For Loop example all the way through
without guessing carefully. Watch how
ichanges on every pass, and notice what valueistill holds after the loop finishes. - Guided Practice (5 min): Reset, switch to the While Loop example,
and predict every line before clicking Check. Pause at the last step and
explain out loud why the loop stops instead of printing
Ready 0. - Assessment (5 min): Predict all six lines of the For Loop example with
no mistakes, then write on paper what
range(3)would print instead.
Assessment
- The student correctly predicts at least five of the six For Loop outputs.
- The student explains why
iequals 4, not 5, afterrange(5)finishes. - The student identifies the line that makes the
whileloop eventually stop. - The student states one difference between a
forloop and awhileloop.
References
- MicroPython Documentation - the official reference for the MicroPython language and its built-in modules.
- Python
forstatement reference - the language definition that MicroPython follows for loops. - Control flow (Wikipedia) - background on loops, conditions, and iteration across programming languages.
- p5.js Reference - the library used to build this MicroSim, useful if you want to modify it.