Changing a global variable with the global keyword

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

Changing a global variable with the global keyword

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. score = 0 def add(): score = score + 1 What happens when add() is called?
A UnboundLocalError
B score becomes 1
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
4. count = 5 def reset(): count = 0 reset() print(count) What is printed?
A 0
B 5
5. count = 5 def reset(): global count count = 0 reset() print(count) What is printed?
A 0
B 5

Match the Pairs

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

Left columnRight column
1global score ALets the function assign to the score variable defined outside all functions
2print(score) Bdef add_point(score): return score + 1
3Declare a variable as global before changing it inside a function CWithout 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.
4Passing a parameter and returning the result is safer than changing a global Ddef add_point(): global score score = score + 1
5score = 0 def add_point(): score = score + 1 EReads a global variable inside a function without needing the global keyword
6def reset(): score = 0 score = 5 reset() FThe 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.

Definitions

global

Fill in the Blanks

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

1.
def bump(): ______ score score += 1
2.
def reset(): global score score = _
3.
lives = 3 def show(): print(_____)
4.
try: add() except _________________:
5.
def bump(score): ______ score + 1 score = bump(score)

Spot the Error

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

Question 1
A score = 0
B def add_point():
C globl score
D score = score + 1
E add_point()
Question 2
A count = 0
B def increase()
C global count
D count += 1
Question 3
A def reset_total():
B global total = 0
C total = 0
D total = 50
E reset_total()
F print(total)
Question 4
A level == 1
B def level_up():
C global level
D level += 1
Question 5
A points = 0
B def win():
C global points
D points + 10
E win()
F print(points)

Answer Key

Changing a global variable with the global keyword
Section A: Quick Questions
Q1
B — global score
Q2
A — UnboundLocalError
Q3
A — No, reading a global works without it
Q4
B — 5
Q5
A — 0
Section B: Match the Pairs
Q1
global score → Lets the function assign to the score variable defined outside all functions
Q2
print(score) → Reads a global variable inside a function without needing the global keyword
Q3
Declare a variable as global before changing it inside a function → def add_point(): global score score = score + 1
Q4
Passing a parameter and returning the result is safer than changing a global → def add_point(score): return score + 1
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def bump(): global score score += 1
Q2
def reset(): global score score = 0
Q3
lives = 3 def show(): print(lives)
Q4
try: add() except UnboundLocalError:
Q5
def bump(score): return score + 1 score = bump(score)
Section E: Spot the Error
Q1
C — The keyword is misspelt, which causes a SyntaxError. It must be 'global score' so the function can change the outside variable.
Q2
B — A function header must end with a colon. It should be 'def increase():'.
Q3
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.
Q4
A — '==' compares and does not assign, so level is never created and the function fails with a NameError. It should be 'level = 1'.
Q5
D — 'points + 10' works out a value but never stores it, so the global does not change. It should be 'points += 10'.