Edexcel 1CP2 6.6.3: Local and global variables
The free BugBot lesson that teach this point, a robot to try it on, and the past-paper questions that test it.
What it covers
A variable made inside a function is not the same as one made outside it, even when they have the same name. Where a variable can be used is called its scope. Getting scope wrong gives some of the most confusing errors in Python; getting it right is what lets functions be written and tested one at a time.
- Local variables stay inside
- Same name, different variables
- Reading a global works; changing it does not
- Fix 1: pass it in and return it
- Fix 2: the global keyword
Or do it in the simulator, with tasks that mark themselves
Lessons that teach it
- F4.3 Local and global variables Functions and structured code
Scope: where a variable exists, and why passing values beats global.
Try it on a robot
The task from F4.3 Local and global variables, running here. Change the program and press Run.
Past paper questions on this
Real exam questions that test this, each worked through with a program you can run.
- AQA 8525 June 2024 Paper 1, Question 10: Trace three subroutines that call each other 2 marks
- AQA 8525 June 2024 Paper 1, Question 14: Local variables, and a subroutine that counts busy days and returns the count 7 marks
- AQA 8525 June 2022 Paper 1, Question 11: Complete a subroutine that works out a sound file's size, local variables, why subroutines 10 marks
- Edexcel 1CP2 June 2023 Paper 2, Question 5: A user ID: local and global names, a procedure, lower(), isdigit() and ord() 15 marks
The specification is Edexcel's own document; this page says which of our lessons teach point 6.6.3 and is not endorsed by the board.