← VCE Software Development U3O1

Changing a global variable with the global keyword

A keyword used inside a function so it can assign to a variable defined outside all functions, instead of creating a new local variable with the same name.

Understanding Changing a global variable with the global keyword in VCE Software Development U3O1

What it does: The global line comes before the assignment, so score = score + 1 changes the outer score rather than making a local one. The function only uses its own parameter and returns a result, so it cannot change other parts of the program by accident. This makes it easier to test and debug. A function can read a global variable freely. The global keyword is only needed when the function assigns a new value to it.

Common mistakes: The keyword is misspelt, which causes a SyntaxError. It must be 'global score' so the function can change the outside variable. A function header must end with a colon. It should be 'def increase():'.

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 72–96) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A function needs to change a variable defined outside all functions. Which line goes at the top of the function?
global scorelocal score
Matches
score = 0 def add_point(): score = score + 1
↓
The global keyword is missing, so Python treats score as local and raises an UnboundLocalError when it is read before assignment. Add global score as the first line inside the function.
Applying
Meanings
Term
global
Meaning
A keyword used inside a function so it can assign to a variable defined outside all functions, instead of creating a new local variable with the same name.
Practice
score = 0
Testing
Gaps
score
Check Answer
Spots
2def add_point():
3 globl score

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

The 'Puffin' badge — Definition Diver, 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 function needs to change a variable defined outside all functions. Which line goes at the top of the function?
A. local score
B. global score ✓
The global keyword tells Python that score refers to the variable outside the function, so assigning to it changes that variable. Python has no local keyword for this.
2. score = 0 def add(): score = score + 1 What happens when add() is called?
A. UnboundLocalError ✓
B. score becomes 1
Assigning to score makes it local in add(). The right-hand side reads it before it has a value, so Python raises UnboundLocalError. Adding global score fixes this.
3. score = 0 def show(): print(score) Does show() need the global keyword to print score?
A. No, reading a global works without it ✓
B. Yes, global is needed for any access
A function can read a global variable freely. The global keyword is only needed when the function assigns a new value to it.
4. count = 5 def reset(): count = 0 reset() print(count) What is printed?
A. 0
B. 5 ✓
Without global, count = 0 creates a new local variable inside reset(). The outer count is unchanged, so it still prints 5.
5. count = 5 def reset(): global count count = 0 reset() print(count) What is printed?
A. 0 ✓
B. 5
global count makes the assignment change the outer variable, so count becomes 0.

Match the Pairs

global score → Lets the function assign to the score variable defined outside all functions
print(score) → Reads a global variable inside a function without needing the global keyword
Declare a variable as global before changing it inside a function → def add_point(): global score score = score + 1
Passing a parameter and returning the result is safer than changing a global → def add_point(score): return score + 1
score = 0 def add_point(): score = score + 1 → The global keyword is missing, so Python treats score as local and raises an UnboundLocalError when it is read before assignment. Add global score as the first line inside the function.
def reset(): score = 0 score = 5 reset() → Without global, score = 0 creates a new local variable and the outer score stays 5. Add global score inside reset() so the outer variable is changed.

Key Term

global
A keyword used inside a function so it can assign to a variable defined outside all functions, instead of creating a new local variable with the same name.

Fill in the Blanks

1. def bump(): global score score += 1
Hint: Type the keyword that lets the function change a variable defined outside it.
2. def reset(): global score score = 0
Hint: Type the number a score is set back to when a game is reset.
3. lives = 3 def show(): print(lives)
Hint: Type the variable name a function can read from outside without needing any special keyword.
4. try: add() except UnboundLocalError:
Hint: Type the error name raised when a function assigns to a name that the function has not declared as outside.
5. def bump(score): return score + 1 score = bump(score)
Hint: Type the keyword that sends a new value back to the caller, which is safer than changing an outside variable.

Spot the Error

Question 1
A score = 0
B def add_point():
C globl score
D score = score + 1
E add_point()
Line C — The keyword is misspelt, which causes a SyntaxError. It must be 'global score' so the function can change the outside variable.
Question 2
A count = 0
B def increase()
C global count
D count += 1
Line B — A function header must end with a colon. It should be 'def increase():'.
Question 3
A def reset_total():
B global total = 0
C total = 0
D total = 50
E reset_total()
F print(total)
Line B — A global statement only declares the name and cannot assign a value. It should be 'global total', with 'total = 0' on its own line.
Question 4
A level == 1
B def level_up():
C global level
D level += 1
Line A — '==' compares and does not assign, so level is never created and the function fails with a NameError. It should be 'level = 1'.
Question 5
A points = 0
B def win():
C global points
D points + 10
E win()
F print(points)
Line D — 'points + 10' works out a value but never stores it, so the global does not change. It should be 'points += 10'.

Student with a class code? Join your class →