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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
local variableFill in the Blanks
Spot the Error
Student with a class code? Join your class →