Local vs Global Scope Explorer
Run the Local vs Global Scope Explorer MicroSim Fullscreen
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
About This MicroSim
Every variable in a program lives somewhere. Scope is the part of the program where a variable can be seen and used. This MicroSim draws scope as boxes, so you can see it.
- The big blue box is the global scope, the whole program. Variables made outside every function live here as blue cards.
- The smaller orange box appears only while a function runs. Variables made inside the function are local variables. They live here as orange cards.
When the function ends, its box slides away and its orange cards vanish in a puff of smoke. If the program then tries to use one of those local names, a red NameError tag pops up.
The three examples come straight from the chapter:
- Local variable:
compute_speed()makes a local variable calledscaled. - Global with the
globalkeyword:start_motors()changes the global flagis_moving. A dashed arrow shows the function reaching the global card. - Global without
global(the bug): the same function without thegloballine. Python makes a new localis_moving, and the global one never changes.
How to Use
- Choose an example from the Example menu. Example 1 is loaded first.
- Press Step to run one line. The yellow bar marks the line that just ran. The message box explains that line in one sentence.
- Press Run to play one line per second, or Reset to start over.
- Watch the output console under the code. It shows what
print()displays. - In examples 2 and 3, use the Show global keyword checkbox to switch
between the working code and the buggy code. It adds or removes a single
line:
global is_moving.
This MicroSim goes with the functions section of Chapter 4: Control Flow, Functions, and Exception Handling.
Lesson Plan
Grade Level
Grades 8–12 (introductory programming with a physical robot)
Duration
15 minutes
Prerequisites
- Defining and calling a function with
def(Chapter 4, "Defining a Function") - Parameters and return values (Chapter 4, "Parameters and Return Values")
- Assigning and printing variables (Chapter 3: MicroPython and Development Environment Setup)
Learning Objective
Students will be able to explain (Bloom's Taxonomy: Understand) why a local
variable disappears after a function ends, and why the global keyword is
needed to change a global variable from inside a function.
Activities
- Predict the error (3 min). Show example 1 before stepping. Ask: "Will
print(scaled)on the last line work?" Record a class vote. - Step through example 1 (4 min). Students step line by line and describe
what happens to
raw_valueandscaledwhenreturnruns. They connect the NameError to the empty function box. - Compare examples 2 and 3 (5 min). In pairs, one student steps through
example 2 and the other steps through example 3. They compare the final
output line (
After call: Truevs.After call: False) and find the one line that makes the difference. - Explain it back (3 min). Each student writes two sentences: one
explaining why
scaleddisappears, and one explaining whatglobal is_movingtells Python.
Assessment
- Challenge: Run example 3, then find the one-line fix. Answer: add
global is_movingas the first line insidestart_motors(). Students check it by turning on Show global keyword and confirming the blue card becomesTrue. - Exit ticket: "Your robot's
obstacle_detectedflag never turnsTrue, even though a function sets it. What is the most likely cause?" Expected answer: the function is missingglobal obstacle_detected, so it made a local variable instead. - Rubric (4-point): Exemplary explains both behaviors using the words
"local scope" and "global scope" and predicts the output of a new example.
Proficient explains both behaviors correctly. Developing explains the
NameError but not the missing-
globalbug. Beginning believes all variables are visible everywhere.
References
- Chapter 4: Control Flow, Functions, and Exception Handling - the
compute_speed()andstart_motors()examples used in this MicroSim. - Python Tutorial: Defining Functions - explains that assignments inside a function create local variables.
- Python Language Reference: The global statement - the official definition of
global. - Scope (computer science) (Wikipedia) - background on where names are visible in a program.
- Variable Scope Explorer (Moving Rainbow) - the earlier MicroSim whose nested scope boxes this one adapts.