Coding a model with functions – Year 9 Computing lesson
Pupils learn to write a Python simulation that uses random numbers in a function and a loop to predict the length of a queue.
Lesson 3 of 6 in unit 9.5 Modelling the Real World, taught in Year 9, Summer 1. It answers one big question: How can a program use chance to predict a lunch queue?
By the end, pupils can write a Python simulation that uses random numbers in a function and a loop to predict the length of a queue.
- The Build (the main task)
- Write a Python simulation of 20 minutes of the lunch queue: a function returns a random number of arrivals each minute, two pupils are served each minute, and the program prints the longest queue.
- Support version
- Parsons problem: the simulation's lines are jumbled, with one extra line that uses range-style end points in randint.
- Challenge
- Run the simulation five times, explain why the answers differ, then change the serving rate to 3 and compare.
- Real-world link
- Pupils code the lunch queue model from lesson 1 and run it to predict the longest queue the catering manager should expect.
- Key words
- Simulation, Random number generation and Probability, each with a definition, an icon and a common mix-up on the key vocabulary slide.
- Misconceptions it tackles
- “random.randint(1, 6) never gives 6, just as range(1, 6) stops before 6”; “A simulation tells you exactly what will happen”.
- National Curriculum
- NC1 (design, use and evaluate abstractions that model real-world problems and physical systems); NC3 (two or more languages, at least one textual; data structures; modular programs with procedures or functions).
Every lesson follows the same Code & Commerce structure: a Bell Task recall starter, key vocabulary with definitions and icons, short teaching chunks with a check before pupils move on, The Build (one main task), challenge questions that get harder, and an exit question. Answers reveal one at a time on the slides, and everything is editable PowerPoint and Word.
Pupils need computers for the main task.
Also sold in the Modelling the Real World unit bundle, which includes the medium term plan, and in the full-year and complete-course bundles.
What's included
- Slide deck (PowerPoint)
- Lesson plan (Word)
- Worksheet (Word)
- Support worksheet with scaffolds (Word)
- Single-teacher licence: use and adapt it with your own classes
More for KS3 Computing
See allPupils learn to use abstraction to decide which details a model needs, and state the assumptions it makes.
Free sample lessonPupils learn to describe a state transition diagram for a real system as a typed state table, with the rule that triggers each transition.
Pupils learn to explain how a machine learning model is trained on example data, test its predictions and spot bias in its training data.
Written for the specifications named. Not endorsed by or affiliated with any exam board.