Local and global variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Local and global variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A variable is created inside a function. Where can it be used?
A Only inside that function
B Anywhere in the program
2. Which program causes a NameError?
A def game(): score = 5 game() print(score)
B score = 5 def game(): print(score) game()
3. Which variable is a global variable?
A name = "Sam" written outside all functions
B name = "Sam" written inside def greet():
4. True or false: a local variable still exists after its function has finished running.
A True
B False
5. A function needs to use a value from outside it. What is a good way to give it that value?
A Hope Python finds the local variable from another function
B Pass it in as a parameter, like def show(points):

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def show(): score = 5 print(score) Aname is a global variable, made outside all functions, so greet() can use it
2name = "Sam" def greet(): print(name) Bdef double(num): print(num * 2)
3A variable created inside a function is local and only exists while that function runs CNameError: score is a local variable made inside show(), so it does not exist outside the function
4Use a parameter to pass a value into a function so it does not need to use a variable from outside Ddef add(): total = 2 + 3
5def make(): age = 12 make() print(age) Eage is local to make(), so print(age) outside the function causes a NameError. Fix: print it inside the function or return it.
6def show(): print(points) show() Fpoints was never created, so show() causes a NameError. Fix: create points outside the function first or pass it in as a parameter.

Definitions

local variable

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def show(): total = 5 print(_____)
2.
___ add_bonus(): bonus = 10 print(bonus)
3.
score = 0 def show(): print(_____)
4.
def double(___): print(num * 2) double(4)
5.
def make(): x = 3 ______ x

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def show():
B score == 10
C print(score)
D show()
Question 2
A def double(n):
B result = x * 2
C return result
D print(double(5))
Question 3
A lives = 3
B def show():
C print(live)
D show()
Question 4
A def make():
B x = 7
C print(y)
D make()
Question 5
A def hello(name):
B print("Hi", Name)
C hello("Mia")

Answer Key

Local and global variables
Section A: Quick Questions
Q1
A — Only inside that function
Q2
A — def game(): score = 5 game() print(score)
Q3
A — name = "Sam" written outside all functions
Q4
B — False
Q5
B — Pass it in as a parameter, like def show(points):
Section B: Match the Pairs
Q1
def show(): score = 5 print(score) → NameError: score is a local variable made inside show(), so it does not exist outside the function
Q2
name = "Sam" def greet(): print(name) → name is a global variable, made outside all functions, so greet() can use it
Q3
A variable created inside a function is local and only exists while that function runs → def add(): total = 2 + 3
Q4
Use a parameter to pass a value into a function so it does not need to use a variable from outside → def double(num): print(num * 2)
Q5
def make(): age = 12 make() print(age) → age is local to make(), so print(age) outside the function causes a NameError. Fix: print it inside the function or return it.
Q6
def show(): print(points) show() → points was never created, so show() causes a NameError. Fix: create points outside the function first or pass it in as a parameter.
Section C: Definitions
Q1
local variable — A variable created inside a function. It only exists in that function, so using it outside gives a NameError. A variable made outside all functions is global and can be used anywhere.
Section D: Fill in the Blanks
Q1
def show(): total = 5 print(total)
Q2
def add_bonus(): bonus = 10 print(bonus)
Q3
score = 0 def show(): print(score)
Q4
def double(num): print(num * 2) double(4)
Q5
def make(): x = 3 return x
Section E: Spot the Error
Q1
B — A single = is needed to create the local variable. With ==, score is never created, so print(score) gives a NameError. It should be score = 10.
Q2
B — x is not a parameter or a variable made in this function, so Python gives a NameError. The function should use the parameter n: result = n * 2.
Q3
C — The global variable is called lives, but live was typed. That name does not exist, so there is a NameError. It should be print(lives).
Q4
C — The only local variable made in make() is x. The name y was never created, so Python gives a NameError. It should be print(x).
Q5
B — Python is case sensitive. The parameter is name, not Name, so Name causes a NameError. It should be print("Hi", name).