Lesson 5:
Round and round
Year 8 – Intro to Python programming
Starter activity
Making predictions
count = 3 Question .
print(count) What will this program display when it is executed?
count = count-1
A. There is no valid value for count. This will
print(count) 1 result in an error.
B. It will print 2.
2C. It will print 3.
3D. It will print 3 and then 2.
▹ 4
Starter activity
Making predictions
count = 3 Assignments are not equations.
print(count) Assignments are instructions to be executed.
count = count-1
This is an instruction to:
print(count)
evaluate the expression count-1 and
assign the value back to count
(replacing the previous value).
? This is an instruction to:
decrease the value of count by 1.
Starter activity
Making predictions: answer
count = 3 Question .
print(count) What will this program display when it is executed?
count = count-1 2
print(count)
State . Output .
count 3 3
count 2 2
Objectives
In this lesson, you will...
● Use iteration (while statements) to allow the flow of program execution to include
loops
● Use variables as counters
Activity 1
Count
count = 3 Question .
print(count) What will this extended program display when it is
count = count-1 executed?
The previous program
print(count) ended here.
count = count-1 Use your worksheet to answer.
Then, move on to the next tasks.
print(count)
count = count-1
Activity 1
Count: walk-through
count = 3 Question .
print(count) What will this extended program display when it is
count = count-1 executed?
print(count)
State . Output .
count = count-1
print(count) count 3 3
count = count-1
count 2 2
count 1 1
count 0
Activity 1
Count: iterative
count = 3 count = 3
print(count) while count >= 1:
count = count-1 print(count)
print(count) count = count-1
count = count-1
print(count)
count = count-1
Activity 1
Count: solutions
count = 3 count = 10 count = 1
while count >= 1: while count >= 1: while count <= 20:
print(count) print(count) print(count)
count = count-1 count = count-1 count = count+3
print("Lift off!")
Activity 2
Times tables practice
Use pair programming .
Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.
Alternate between roles.
Activity 2
Times tables practice: a single question
from random import randint
a = randint(2,12) Generate two random integers a and b .
b = randint(2,12)
print(a, "times", b, "=") Ask the user for the product of a and b .
answer = int(input())
product = a * b Calculate the correct answer .
if answer == product: Check the user’s answer and provide feedback .
print("That is correct")
else:
print("I am sorry")
print(a, "times", b, "is", product)
Activity 2
Times tables practice: multiple questions
Start from your current program.
Complete the tasks in your worksheet to
build a more complete times tables
practice game.
Activity 3
Some code reading
from random import randint from random import randint
rolls = 0 rolls = 0
sixes = 0 sixes = 0
while rolls < 10: while sixes < 10:
dice = randint(1,6) dice = randint(1,6)
if dice == 6: if dice == 6:
sixes = sixes + 1 sixes = sixes + 1
rolls = rolls + 1 rolls = rolls + 1
The only difference between these two programs is the condition in
the while loop. When does each loop terminate?
Summary
In this lesson, you... Next lesson, you will...
Used iteration (while statements) to Apply what you’ve learnt and
allow the flow of program execution to use iteration even more!
include loops
Take a quiz, to assess what you’ve learnt
Used variables as counters