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