← Years 7–8 Creating Digital Solutions

Local and global variables

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.

Understanding Local and global variables in Years 7–8 Creating Digital Solutions

What it does: total is made inside add(), so it is a local variable that cannot be used outside add(). The parameter num receives a value when the function is called, so the function can work with it without needing a global variable. In the first program, score is local to game(), so print(score) outside the function fails with a NameError. Local variables disappear when the function ends, so you cannot use them afterwards.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 50–125) and word search (Modules 3–125).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A variable is created inside a function. Where can it be used?
Anywhere in the programOnly inside that function
Matches
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.
Applying
Meanings
Term
local variable
Meaning
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.
Practice
def add_bonus(score):
Testing
Gaps
print()
Check Answer
Spots
1def show():
2 score == 10

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hyena' badge — Savannah Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A variable is created inside a function. Where can it be used?
A. Only inside that function ✓
B. Anywhere in the program
A local variable only exists inside the function where it was created.
2. Which program causes a NameError?
A. def game(): score = 5 game() print(score) ✓
B. score = 5 def game(): print(score) game()
In the first program, score is local to game(), so print(score) outside the function fails with a NameError.
3. Which variable is a global variable?
A. name = "Sam" written outside all functions ✓
B. name = "Sam" written inside def greet():
A variable made outside all functions is global. One made inside a function is local.
4. True or false: a local variable still exists after its function has finished running.
A. True
B. False ✓
Local variables disappear when the function ends, so you cannot use them afterwards.
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): ✓
Parameters let you pass values into a function. Local variables from other functions cannot be seen.

Match the Pairs

def show(): score = 5 print(score) → NameError: score is a local variable made inside show(), so it does not exist outside the function
name = "Sam" def greet(): print(name) → name is a global variable, made outside all functions, so greet() can use it
A variable created inside a function is local and only exists while that function runs → def add(): total = 2 + 3
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)
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.
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.

Key Term

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.

Fill in the Blanks

1. def show(): total = 5 print(total)
Hint: Type the name of the local variable created inside the function so it can be printed.
2. def add_bonus(): bonus = 10 print(bonus)
Hint: Type the keyword that starts a function, which is where the local variable bonus lives.
3. score = 0 def show(): print(score)
Hint: Type the name of the global variable that was created outside the function.
4. def double(num): print(num * 2) double(4)
Hint: Type the parameter name that receives the value passed in and is used inside the function.
5. def make(): x = 3 return x
Hint: Type the keyword that sends a value out of the function, because the local variable x cannot be used outside.

Spot the Error

Question 1
A def show():
B score == 10
C print(score)
D show()
Line 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.
Question 2
A def double(n):
B result = x * 2
C return result
D print(double(5))
Line 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.
Question 3
A lives = 3
B def show():
C print(live)
D show()
Line 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).
Question 4
A def make():
B x = 7
C print(y)
D make()
Line 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).
Question 5
A def hello(name):
B print("Hi", Name)
C hello("Mia")
Line B — Python is case sensitive. The parameter is name, not Name, so Name causes a NameError. It should be print("Hi", name).

Student with a class code? Join your class →