Skip to content

Latest commit

 

History

History
106 lines (70 loc) · 5.14 KB

File metadata and controls

106 lines (70 loc) · 5.14 KB

Lesson 3.04: Debugging and Scope

Learning Objectives

Students will be able to...

  • Define and identify scope, aliasing, stack diagram, stack trace.
  • Demonstrate that changing a list inside a function updates the list outside of the function.
  • Demonstrate that updating variables inside a function does not affect the variable outside of the function.
  • Demonstrate the use of global variables.
  • Draw a simple stack diagram.

Materials/Preparation

Pacing Guide

Duration Description
5 Minutes Do Now
20 Minutes Lesson
20 Minutes Lab/Review
10 Minutes Debrief

Instructor's Notes

1. Do Now

  • Students have a chance to think about what & discuss what concepts they have been most challenged by.
  • Next, students practice passing a list as an argument and updating that list within the function.

2. Lesson

  • Discuss what students observed in the Do Now and take time, if needed, to go over questions about concepts that students find challenging.

Aliasing

  • Explain the concept of aliasing.

  • You can draw on the board a diagram of the variable pointing to a list.

  • Note that when passing the location of a list you are not passing the actual value, so the list can be changed.

  • Video Explanation of Aliasing:

    Python - aliasing

Scope of functions

  • Explain to students that variable scope is the part of a program where a variable is accessible.

  • A variable which is defined in the main body of a file is called a global variable.

  • Video explanation of Variable Scope

    Python - Scope

Global Variables, Constants and Local Variables

  • Explain global variables are often used for constants.
  • Any variable created inside of a function is a local variable.
  • Variables in functions include the function parameters, the variables defined in the function, and variables declared as global.
  • Local variables of functions can't be accessed from outside when the function call has finished.

Conventions

Stack Diagrams

  • Demonstrate how to draw the Stack Diagrams shown in the course book (found in section 3.4) and explain how they show the scope of variables as they related to functions.
  • Point out the error messages that will occur if you use a variable out of its scope.

Stack Traces

  • Show an example of a stack trace for a simple error in the Do Now Part 2 sample code.
  • The error message shows evidence of how Python keeps track a "stack" of functions, each one calling the next one, so that it can make available only the variables that are in scope in the current function.

Debugging

  • Help students follow their program to understand how the code is working.
  • Explain how the use of print statements throughout your code can let you know where in the program things are not operating as expected.

3. Lab

  • This lab has students running code that gets them thinking about aliasing and scope. They must also create a stack diagram for a program to show their understanding of scope.

4. Debrief

  • Take time to review the concepts covered today: scope, aliasing, and stack diagrams.
  • Call a few students to the board to draw their stack diagrams from the lab and talk through them.

Accommodation/Differentiation

If students are moving quickly, they can look ahead at the project spec or research the game Oregon Trail for context.