In lesson 1, students learnt that computer programs are written as a sequence of codes and that computers follow those codes exactly. They also started to use the Python turtle library to create simple graphics.
In the 2nd lesson, students will learn about loops and variables, and use them in their competition entries.
Ask students why Python is called Python. If none of them have seen any Monty Python, try showing them some videos you think they'd like. One of our favourites is the bus animation
Ask students if any want to show some of the graphics they found and ask if they understood the code. A lot of the good stuff involves loops, which we'll be covering today. But before we can use loops, we need to understand variables.
Start off by playing a version of hangman on the white board. This version teaches the importance of variables.
Don't record the wrong guesses, and have the students in a line, come up one by one to the board. They whisper their guess and if it's right, add the letter. If it's wrong tell them it's wrong but don't record it.
Play for a while and ask why it's harder than normal. The reason is that nobody knows all the wrong guesses because they haven't been recorded.
As people, we take our short term memory for granted, but computers must be told explicitly to remember anything.
Variables have 3 important features:
The variables we'll be using will be storing numbers.
We use the single
= to assign a number to a variable. Variables can be called pretty much anything, but there can be no spaces in the name. It's a good idea to choose a name that matches the variable's use. So for now, when we're testing what variables do, I'm calling it test_var.
When we use the
+ to add to the variable, we are using an
operator. Common mathematical operators include
+, -, /, *.
Ask students to guess what will happen when we run the code.
Ask the students to start Idle and make a new file. Ask them to type the variables code in the handout and save it as variables.py. Ask them to save and run their file. Remember to add the
.py to the end of their filename.
Discuss what happened. Did the students predict right? It's great practice to predict what will happen before running code.
Ask students to look at the looping code in their handouts.
Ask them to guess how it works.
Ask them to tell you what they think will happen as it runs through the loop.
Explain that line 2 starts the loop with the
while keyword. The following lines will be looped as long as the condition
loops < 20 is True.
Show them that lines 3 to 5 are indented, and this is how Python knows that they are the sequence of codes for while to repeat.
1 2 3 4 5
Ask the students to type in the code looping from their handouts. This can be added to the end of their current work in variables.py. Get them to check it does what they expect.
Encourage the students to use variables and loops in their turtle code. The following picture shows you how to spot the ideas students are using.
You may notice that some students are having problems shutting their windows. This will because their programs are no longer running the
done() command because their loops never end. Also, some students will put the
done() command inside their loop, so the program stops every loop.
Now we've introduce loops, some pictures may take some time to draw. Encourage students to use the
speed(0) command from the first handout to make the drawing happen as fast as possible.
Ask your students to investigate a loop within a loop:
1 2 3 4 5 6 7 8
These can add extra detail to drawings.
Ask how a computer remembers data. Ask for an example of when this is necessary - for instance counting the number of loops have already been run.
Ask how we repeat computer code. Ask why looping is important in coding. What Python keyword do we use for creating a loop? The
Students are asked to investigate a new way of drawing by choosing something from the documentation. They should only choose from the 'Move and draw' section.