L3 Lesson plan - Intro to Python programming - Y8
L3 Lesson plan - Intro to Python programming - Y8
Lesson 3 - At a crossroads
Lesson 3: At a crossroads
Introduction
This lesson introduces selection and randomness. These are two features that
will allow learners to develop programs with a very diverse range of behaviours.
Learners will revisit some of the programs that they have encountered in
previous lessons and extend them into more versatile programs that use
selection. They will develop a simple number guessing game, which will
eventually include randomness.
In this lesson, selection is limited to binary choices, i.e. there are always only two
possible branches. Multiple branches and nested structures will be introduced
later on.
Learning objectives
● Use relational operators to form logical expressions
● Use binary selection (if, else statements) to control the flow of program
execution
● Generate and use random integers
Key vocabulary
Selection, relational (or comparison) operators, logical (or Boolean) expressions,
conditions, randomness, execution, walk-through
Preparation
Subject knowledge:
● You will need to be familiar with using a Python IDE.
● You will need to be able to locate and correct syntax errors in Python
programs.
● You will need to be comfortable with the use of output, input, and
assignment in Python, including arithmetic input.
● You will need to be comfortable with the use of arithmetic and relational
operators, and arithmetic and logical expressions (conditions).
● You will need to be comfortable with the use of selection in Python
programs.
Assessment opportunities
Closed-formed questions throughout the lesson provide a means for quick
formative assessment on specific concepts. You can also assess learners’
answers to the worksheets. In addition, you can assess learners through
observation, for example, by assessing how learners interact through pair
programming and collaborate to solve problems.
At a glance
12 mins Show the general form of the if statement in Python and explain
when it is necessary. Use live coding to build a first example that
employs selection, along with your learners.
20 mins Ask learners to carry out the ‘Film critic’ and ‘Lucky number’ tasks in
the ‘Practise using selection’ worksheet. They can use the code in
the worked example included in the worksheet as a point of
reference.
8 mins Display the code for the number guessing game, and identify and
label the distinct sections in the program. Perform a walk-through of
3 mins labels the part of the program that contains the selection structure, step by
5 mins walk-
through step, along with your learners.
Activity 4 Randomness
10 mins Use live coding to extend the number guessing game to pick a
random lucky number. Use a couple of multiple choice questions on
4 mins generating random integers for a dice rolling program and allow
live coding
2 mins learners some time to type the code and execute it a few times, so
questions that they can see how a random number is generated each time.
4 mins task
Activity 5 Plenary
Outline plan
Please note that the slide deck labels the activities in the top right-hand corner
to help you navigate the lesson.
Then, describe a ‘what if’ scenario that would involve extending the
program to execute different instructions, depending on the input.
For example: “This program greets every user by name. What if we
wanted the program to recognise one particular name and treat it
differently?”
Use live coding to build the first example of using selection, while
your learners follow. Start with the existing program that greets the
user by name and extend it.
After writing your program, turn your attention to the finer points of
the if statement syntax in Python (e.g. no capitalisation of the
keywords if and else, the colon, the equality relational operator,
and — crucially — indentation). Display slide 10 to highlight the
‘syntactic pitfalls’ of the if statement in Python.
Ask learners to run and test their programs. While they are doing
that, hand out the ‘Practise using selection’ worksheet. The worked
example on the first page contains the correct version of the code.
There is also a checklist; instruct learners to use it, in order to
check for and correct possible syntax errors.
Briefly read through the reference slide (slide 11) that contains the
relational operators (comparisons) that can be used in conditions.
8 mins Display slide 15, which shows the code for the number guessing
3 mins labels game, then click once to activate the animation that will identify
5 mins walk- the distinct sections in the program. At first, do not describe what
through each section does. Ask learners to suggest labels for these
sections that summarise their function. Then, click through the
animations to provide examples of what these labels might look
like for these particular sections.
Activity 4 Randomness
(Slides 18–
22) Keep displaying the code for the number guessing game (slide 18).
Describe a ‘what if’ scenario that would involve extending the
10 mins program to pick a random lucky number every time it is executed.
4 mins live Use live coding to extend the program, while your learners follow.
coding
2 mins In your program, insert a print statement to display the lucky
questions number right after it is generated. This will allow you to see the
4 mins task
lucky number and test that your program behaves correctly. It will
also model a useful testing/debugging method for your learners.
Remember to remove the line you added after you have finished
testing.
Allow learners some time to type the code for the dice rolling
program and execute it a few times, so that they can see how a
random number is generated each time (slide 22).
Activity 5 Plenary
(Slides 23–
25) Ask a few closed-form questions (true/false, multiple choice) to
assess how comfortable learners are with randomness and the
10 mins selection structure.
Notes on pedagogy
Common misconceptions
Throughout these lessons, you should be aware of the common misconceptions
that may arise with novice programmers. The misconceptions below are a
selection from Appendix A of Juha Sorva’s PhD thesis, Visual Program Simulation
in Introductory Programming Education, and are relevant to this lesson. The list
will help you avoid using any examples or explanations that may give rise to
these misconceptions, and it will also help you spot them, should they arise with
your learners.
M1, 2, 7: The computer knows or is able to deduce the intention of the program
or of a piece of code, and acts accordingly. The machine understands English.
M6: Difficulties with telling apart the static and dynamic aspects of programs.
M24: Code after if statement is not executed if the then clause is.
M27, 29: Both then and else branches are executed. Using else is optional (the
next statement is always the else branch).
M161: Boolean values are just something used in conditionals and not data
comparable to numbers or strings.
Throughout this unit, simple animations are used to visualise the execution of
Python programs, using ‘sketches’ of variables and their values to illustrate how
program state (e.g. the values of variables, and the contents of data structures)
is modified during program execution. This can provide a visual alternative to
trace tables that may be less cumbersome for learners at this stage.
Subgoal labelling
Subgoals “group a set of solution steps by their purpose” [source]. Subgoal labels
are often used in worked examples, to help learners “create a framework for
problem-solving” [source]. You can find a short summary about subgoal labels, as
well as links to relevant research, in Greg Wilson’s book Teaching Tech Together,
in the section on cognitive load.
Subgoal labels also relate directly to the Blocks and Relations levels of Schulte’s
Block Model, mentioned in the previous lesson. Subgoal labels help learners
abstract away from the Atoms level, i.e. individual statements, and understand
how blocks of statements form problem-solving units, and how these blocks
interact with each other.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
Attribution statement
This resource was created by Raspberry Pi Foundation and updated by STEM Learning for the
National Centre for Computing Education.
The contents of this resource are available for use under the Open Government License (OGL v3)
meaning you can copy, adapt, distribute and publish the information. You must acknowledge the
source of the Information in your product or application, by attributing Raspberry Pi Foundation and
STEM Learning as stated here and are asked to provide a link to the OGL v3.
The original version can be made available on request via [email protected].