M&C4: Misunderstanding local vs global variables

All variables that are assigned a value within the function body are local to that function (and are thus local variables). Once the function terminates, the value is no longer available. Students often have difficulties in understanding this.

When a student wants to write a function changing a variable defined outside the function, Python requires the use of the global statement.  Global statements (e.g., global count1) allow a function to change the value of the listed variable so that the change exists after the function terminates. While global statements can be useful, students may forget what is global in what function. This can lead to side effects causing errors later. 

The following video discusses examples and the rules on the global variables in functions. Video Length: 8:58