Year 8 · Y8.3 Python: Loops, Lists and Functions · Lesson 1
Stretch tasks Loops
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
This or That
Pixel the cat asks 8 questions. Each has two answers: pick the right one.
- Two answers to every question
- A fish treat for every right answer
- Streaks win Pixel shades, then a party hat
About 4 minutes
Memory Match
Flip the cards two at a time to match each code card to its output card.
- 12 cards: each code has a output
- One peek at every card
- Combo points for pairs found in a row
6 pairs, about 4 minutes
The games need JavaScript. Every question is below, with the answers at the end.
Loops: the questions
Quiz Challenge, round 1: Quiz Blitz
Type every number this prints, with spaces between them.
for i in range(2, 10, 3): print(i)How many times does this print Hello?
for i in range(1, 6): print("Hello")- 5
- 6
- 4
- 1
range(5) produces the numbers 1, 2, 3, 4 and 5.
- True
- False
Which loop should you use to keep asking for a PIN until it is correct?
- A for loop over range(3)
- No loop: one if statement is enough
- A while loop, because you do not know how many tries it will take
- A for loop, because it repeats a fixed number of times
What happens when this runs?
count = 1 while count < 5: print(count)- It prints 1, 2, 3, 4 and 5
- It prints nothing
- It prints 1 forever, because count never changes
- It prints 1, 2, 3 and 4
What is printed?
total = 0 for n in range(1, 5): total = total + n * n print(total)Put these lines in order to add up 1, 2 and 3 and print the total.
Number these 1 to 4.
for i in range(1, 4):total = total + i(indented)print(total)total = 0
Which TWO loops print the numbers 0 to 4?
Tick 2.
for i in range(4): print(i)for i in range(0, 4): print(i)for i in range(5): print(i)for i in range(0, 5): print(i)for i in range(1, 5): print(i)
What is printed?
x = 10 while x > 0: x = x - 3 print(x)- 1
- 0
- -3
- -2
A for loop can go through every letter of a string, one at a time.
- True
- False
What does range(10, 0, -2) produce?
- 10, 8, 6, 4, 2
- 10, 8, 6, 4, 2, 0
- 0, 2, 4, 6, 8, 10
- Nothing, because 10 is bigger than 0
What is printed on the last line?
word = "loop" for letter in word: print(letter * 2)
Quiz Challenge, round 2: Quick Fire
Explain the difference between a for loop and a while loop. [2 marks]
This loop never ends. Explain why, and how to fix it. [2 marks]
lives = 3 while lives > 0: print("Playing")What is the difference between range(5) and range(1, 6)? [2 marks]
Why is a while loop the right choice for checking a password? [2 marks]
Quiz Challenge, round 3: Big Think
Explain what this program does, and what it outputs if the user enters 4, then 7, then 0. [4 marks]
total = 0 number = int(input("Number: ")) while number != 0: total = total + number number = int(input("Number: ")) print("Total:", total)A student prints the same message five times by writing five print lines instead of using a loop. Explain why loops make programs better, using examples. [6 marks]
This or That: tick the right answer
You want to repeat something exactly 10 times. Which loop?
- while
- for
A game should keep running until the player chooses to quit. Which loop?
- while
- for
What is the last number printed?
for i in range(1, 4): print(i)- 4
- 3
What is printed?
print(list(range(0, 10, 5)))[0, 5][0, 5, 10]
What causes an infinite loop?
- Nothing inside it changes the condition
- The loop has too many lines inside it
What is printed?
n = 0 while n < 6: n = n + 2 print(n)- 6
- 4
Which loop is best for going through every item in a list?
- while
- for
The user types dog, then cat. How many times does input run?
password = "" while password != "cat": password = input("Password: ")- 1
- 2
Memory Match: match each code to its output
| Code | Output |
|---|---|
A list(range(3)) | 1 10 |
B list(range(1, 4)) | 2 [0, 4, 8] |
C list(range(0, 10, 4)) | 3 [1, 2, 3] |
D list(range(5, 0, -2)) | 4 [2] |
E len(range(10)) | 5 [0, 1, 2] |
F list(range(2, 3)) | 6 [5, 3, 1] |
Answers
Round 1: Quiz Blitz
- 2 5 8 Start at 2 and go up in steps of 3: 2, 5, 8. The next step would be 11, which is past the stop value of 10.
- 5 range(1, 6) is 1, 2, 3, 4, 5. The stop value is never included.
- False range(5) is 0, 1, 2, 3, 4: five numbers, starting at 0.
- A while loop, because you do not know how many tries it will take When the number of repeats is unknown, a while loop keeps going until its condition is false.
- It prints 1 forever, because count never changes Nothing inside the loop changes count, so count < 5 stays true forever: an infinite loop.
- 30 1 + 4 + 9 + 16 = 30. Each pass adds n squared.
total = 0→for i in range(1, 4):→total = total + i(indented) →print(total)The total must start at 0 before the loop, and the print goes after the loop so it runs once.for i in range(5): print(i);for i in range(0, 5): print(i)range(5) and range(0, 5) are the same: 0 up to, but not including, 5.- -2 x goes 10, 7, 4, 1, -2. Only when x is -2 is x > 0 false, so the loop stops and -2 is printed.
- True for letter in "hello": visits h, e, l, l, o in turn.
- 10, 8, 6, 4, 2 A negative step counts down. The stop value, 0, is still never included.
- pp Multiplying a string repeats it. The last letter is p, so the last line is pp.
Round 2: Quick Fire
- A for loop repeats a known number of times, or once for each item
- A while loop repeats as long as a condition is true, so the number of repeats can be unknown
Model answerA for loop repeats a known number of times, or once for each item in a collection. A while loop repeats for as long as a condition is true, so it is used when you do not know in advance how many repeats are needed.
- lives never changes, so the condition stays true
- Take 1 off lives inside the loop
Model answerNothing inside the loop changes lives, so lives > 0 is always true and the loop never ends. To fix it, add lives = lives - 1 inside the loop, so lives eventually reaches 0.
- range(5) gives 0 to 4
- range(1, 6) gives 1 to 5
Model answerrange(5) gives 0, 1, 2, 3, 4, starting from 0. range(1, 6) gives 1, 2, 3, 4, 5, starting from 1. Both give five numbers.
- You do not know how many attempts the user will need
- It keeps asking until the password is correct
Model answerYou do not know how many attempts the user will need, so a fixed number of repeats will not do. A while loop keeps asking until the password is correct.
Round 3: Big Think
- It keeps asking for numbers until 0 is entered
- It adds each number to a running total
- When 0 is entered the loop ends, and 0 is not added
- It outputs Total: 11
Model answerThe program keeps asking for numbers until 0 is entered. Each number is added to a running total. When 0 is entered the while condition becomes false, so the loop stops without adding it. With 4, 7 and 0 it outputs Total: 11.
- The code is shorter, with fewer lines
- The number of repeats is easy to change by editing one number
- There are fewer mistakes from copying lines
- A while loop can repeat an unknown number of times
- A loop can process every item in a list, even thousands
- Loops make code easier to read and maintain
- An example, such as for i in range(5)
Model answerA loop makes the code shorter: for i in range(5) followed by one indented print does the job of five lines. If the message had to appear 50 times instead, you would change one number rather than copying 45 more lines, and copying lines is exactly how typos and mistakes creep in. A while loop can also repeat an unknown number of times, for example until the user types quit, which separate lines can never do, and a for loop can process every item in a list even if there are thousands of them. Shorter code is also easier to read and maintain.
This or That
- for A for loop repeats a known number of times. A while loop repeats until a condition becomes false.
- while Nobody knows how many turns there will be, so a while loop keeps going until the player quits.
- 3 range(1, 4) stops before 4, so it gives 1, 2 and 3.
[0, 5]range stops before its end value, so 10 is never included.- Nothing inside it changes the condition If nothing in the loop can make the condition false, it runs forever.
- 6 n goes 2, 4, 6. When n is 6, n < 6 is false, so the loop stops and 6 is printed.
- for for item in my_list visits each item once, with no counter to look after.
- 2 dog is wrong, so the loop asks again. After cat the condition is false and the loop ends.
Memory Match
- A 5
list(range(3)):[0, 1, 2] - B 3
list(range(1, 4)):[1, 2, 3] - C 2
list(range(0, 10, 4)):[0, 4, 8] - D 6
list(range(5, 0, -2)):[5, 3, 1] - E 1
len(range(10)):10 - F 4
list(range(2, 3)):[2]