1 of 14

Lesson 5:

Round and round

Year 8 – Intro to Python programming

2 of 14

Starter activity

Making predictions

count = 3

print(count)

count = count-1

print(count)

Question .

What will this program display when it is executed?

  1. There is no valid value for count. This will result in an error.
  2. It will print 2.
  3. It will print 3.
  4. It will print 3 and then 2.

1

2

3

4

3 of 14

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.

4 of 14

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

5 of 14

Objectives

  • Use iteration (while statements) to allow the flow of program execution to include loops
  • Use variables as counters

In this lesson, you will...

6 of 14

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.

7 of 14

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

8 of 14

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

9 of 14

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

10 of 14

Activity 2

Times tables practice

Use pair programming .

Driver

Control the keyboard and mouse.

Navigator

Provide support and instructions.

Alternate between roles.

11 of 14

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 .

12 of 14

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.

13 of 14

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?

14 of 14

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