Year 8 · Spring 2

Python I · Lesson 5 of 5

for, range and random

PracticalTopic 13 Programming basics

Year 8 · Python I · Lesson 5 of 5

You will learn to

  • Use for and range() to repeat code a set number of times.
  • Explain why range(5) gives 0, 1, 2, 3 and 4, and stops before 5.
  • Use import random and random.randint() to make a dice program.
  • Create a times table program in four lines or fewer.

Year 8 · Python I · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • I can predict the numbers that range(5) and range(1, 11) give.
  • I can make a dice that gives a random whole number from 1 to 6.
  • I can print a times table with a for loop.
  • I can say when to use for and when to use while.

Year 8 · Python I · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    What does 17 % 5 give?

    A 3.4B 3C 2

    C. 2

  2. Last unit

    Hex F is…

    A 10B 16C 15

    C. 15

  3. Earlier this year

    When you search, a search engine looks in…

    A Its index, built earlier by crawlersB Your browser historyC The live web, right then

    A. Its index, built earlier by crawlers

Year 8 · Python I · Lesson 5 of 5

Key words (1 of 2)

Variable
A named place in memory that holds a value.
Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".

Year 8 · Python I · Lesson 5 of 5

Key words (2 of 2)

Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.
Infinite loop
A loop whose condition never becomes false.

The explanation · step 1 of 1

Loops: for

for i in range(5):
    print("Lap", i)

Prints Lap 0 to Lap 4. range(5) gives 0, 1, 2, 3, 4: it starts at 0 and stops before 5.

e.g.Example

Scratch: 'repeat 5'.

Year 8 · Python I · Lesson 5 of 5

Quick quiz · 1 of 3

How many times does for i in range(2, 10, 2): run?

  1. A 8
  2. B 5
  3. C 4

C. 4

2, 4, 6, 8. The next would be 10, which is the stop, so it isn't used. 4 times.

Year 8 · Python I · Lesson 5 of 5

Quick quiz · 2 of 3

score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?

  1. A Both
  2. B Gold
  3. C Bronze

C. Bronze

45 >= 25 is True, so the first branch runs and the rest is skipped. The order is wrong: a logic error.

Year 8 · Python I · Lesson 5 of 5

Quick quiz · 3 of 3

A program runs with no message but says the average of 4 and 6 is 7. This is a…

  1. A Syntax error
  2. B Logic error
  3. C Runtime error

B. Logic error

It ran but gave the wrong answer: a logic error. (4 + 6 / 2 = 7; it needs brackets.)

Year 8 · Python I · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. count = 5, then while count > 0: print(count). What happens?

    A Prints 5 4 3 2 1B Prints 5 for everC Prints nothing

    B. Prints 5 for ever Nothing changes count, so count > 0 stays True: an infinite loop.

  2. What does print("3" + "4") show?

    A 34B 7C An error

    A. 34 They're strings, so + joins them: 34.

Year 8 · Python I · Lesson 5 of 5

Homework and the challenge

Homework

Study page 13 to the quiz.

End-of-unit challenge

A number-guessing game with limited lives and a replay option.