Skip to content
Computing · Secondary 3

Active learning ideas

Scope of Variables: Local vs. Global

Active learning helps students grasp variable scope because mistakes in tracing access feel immediate and concrete when they run code. Predicting outputs before execution builds careful reading habits that paper examples alone cannot create.

MOE Syllabus OutcomesMOE: Programming - S3
25–40 minPairs → Whole Class4 activities

Activity 01

Code Prediction Challenge: Scope Hunt

Provide printed code snippets with local and global variables. Students predict outputs in pairs, discuss access rules, then test in Python IDLE. Debrief as a class on surprises.

Differentiate between local and global variables in terms of their accessibility.

Facilitation TipDuring Scope Hunt, circulate and ask pairs to explain their predicted outputs step-by-step to uncover hidden assumptions about variable lifetime.

What to look forPresent students with 2-3 short Python code snippets involving both local and global variables. Ask them to write down the predicted output for each snippet and briefly explain their reasoning, focusing on variable accessibility.

RememberUnderstandAnalyzeSocial AwarenessSelf-AwarenessDecision-Making
Generate Complete Lesson

Activity 02

Outdoor Investigation Session40 min · Small Groups

Debugging Relay: Fix Scope Errors

Divide class into small groups. Each group gets buggy code with scope issues. They fix one error, pass to next group. Final groups run and explain fixes.

Predict the output of a program involving both local and global variables.

Facilitation TipFor Debugging Relay, give each group a different bug first to maximize peer discussion before they share fixes with the class.

What to look forPose the scenario: 'Imagine you are building a simple calculator program. When would it be more appropriate to use a global variable for a value like PI, and when would it be better to pass a number to be squared as a parameter to a squaring function?' Facilitate a class discussion on the trade-offs.

RememberUnderstandAnalyzeSocial AwarenessSelf-AwarenessDecision-Making
Generate Complete Lesson

Activity 03

Outdoor Investigation Session35 min · Small Groups

Parameter vs Global Debate: Rewrite Functions

Give functions using globals. In small groups, rewrite with parameters, test both versions. Groups present pros and cons to class.

Justify when it is appropriate to use a global variable versus passing parameters.

Facilitation TipIn the Parameter vs Global Debate, assign roles (pro-parameters vs pro-globals) to force students to defend positions they might not initially hold.

What to look forOn an index card, have students define 'local variable' and 'global variable' in their own words. Then, ask them to write one sentence explaining a situation where using a global variable might lead to a bug.

RememberUnderstandAnalyzeSocial AwarenessSelf-AwarenessDecision-Making
Generate Complete Lesson

Activity 04

Outdoor Investigation Session25 min · Individual

Scope Visualizer: Draw and Code

Individually sketch variable lifetimes on paper, then code matching programs. Share sketches in pairs to check understanding before running.

Differentiate between local and global variables in terms of their accessibility.

Facilitation TipDuring Scope Visualizer, insist students label each variable as local or global on their diagrams before coding to reinforce the connection between visuals and code.

What to look forPresent students with 2-3 short Python code snippets involving both local and global variables. Ask them to write down the predicted output for each snippet and briefly explain their reasoning, focusing on variable accessibility.

RememberUnderstandAnalyzeSocial AwarenessSelf-AwarenessDecision-Making
Generate Complete Lesson

A few notes on teaching this unit

Teachers approach scope by starting with small, predictable snippets where students can see the exact moment a variable disappears. Avoid abstract rules at first; instead, let students experience the NameError when a local is accessed outside its function. Research shows tracing execution beats lectures for retention, so prioritize live debugging over slides. Always connect scope to real bugs students will face, like accidental overwrites or hard-to-find typos in global lists.

Successful learning looks like students confidently predicting outputs, explaining scope boundaries with examples, and choosing parameters over globals in rewritten functions. They should also justify their choices with clear reasoning about modularity and bugs.


Watch Out for These Misconceptions

  • During Code Prediction Challenge: Scope Hunt, watch for students who assume variables created inside any block persist outside, even if the block is a function.

    Have pairs run their predicted code immediately after writing it. When a NameError appears, ask them to circle where the variable was created and where it was accessed to trace the scope boundary.

  • During Debugging Relay: Fix Scope Errors, watch for students who think the global keyword makes all variables global automatically.

    After they fix a bug, ask them to explain why the global keyword was or was not needed in their solution, referencing the exact line where the variable was assigned.

  • During Parameter vs Global Debate: Rewrite Functions, watch for students who claim globals are always easier because they avoid parameters.

    Require them to test their rewritten functions with edge cases (e.g., negative numbers) to see how globals cause hidden changes that break modularity.


Methods used in this brief