Scratch forever and repeat until blocks as while loops

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Scratch forever and repeat until blocks as while loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which Python line matches the Scratch forever block?
A while True:
B for True:
2. Which Python line matches the Scratch block: repeat until score = 10?
A while not score == 10:
B while score == 10:
3. A while True: loop will stop by itself after a few turns.
A True
B False
4. How do you stop a while True: loop when the player types 'quit'?
A if answer == 'quit': continue
B if answer == 'quit': break
5. A Scratch repeat until block checks its condition. When does the loop run its code?
A While the condition is still false
B While the condition is already true

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1while True: AScratch repeat until done means keep going until done is true, so the loop needs not. This loop would stop at the start while done is False. Use while not done:
2while not done: Bwhile not score == 10: score = score + 1
3A forever block that stops when the player presses q can be written with break inside a while True loop CThe Python version of Scratch repeat until done. It keeps looping while done is still False and stops once done becomes True.
4Scratch repeat until score = 10 becomes a while loop that uses not DTrue needs a capital T in Python. Writing true gives a NameError because Python does not know that name. Use while True:
5while true: print("Go!") EThe Python version of the Scratch forever block. It repeats the indented code nonstop until a break stops it.
6while done: print("Working") Fwhile True: key = input() if key == "q": break

Definitions

while True:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ True: print("Hello!")
2.
while ____: print("Go!")
3.
done = False while ___ done: done = True
4.
while True: if score >= 10: _____
5.
lives = 3 while not lives __ 0: lives = lives - 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A score = 0
B while True:
C score == score + 1
D if score == 5:
E break
Question 2
A while True:
B answer = input('Quit? ')
C if answer == 'yes'
D break
Question 3
A lives = 3
B while True:
C lives = lives - 1
D if lives = 0:
E break
Question 4
A energy = 3
B while True:
C energy = energy - 1
D print(energy
E if energy == 0:
F break
Question 5
A tries = 0
B found = False
C while found:
D tries = tries + 1
E found = tries == 3

Answer Key

Scratch forever and repeat until blocks as while loops
Section A: Quick Questions
Q1
A — while True:
Q2
A — while not score == 10:
Q3
B — False
Q4
B — if answer == 'quit': break
Q5
A — While the condition is still false
Section B: Match the Pairs
Q1
while True: → The Python version of the Scratch forever block. It repeats the indented code nonstop until a break stops it.
Q2
while not done: → The Python version of Scratch repeat until done. It keeps looping while done is still False and stops once done becomes True.
Q3
A forever block that stops when the player presses q can be written with break inside a while True loop → while True: key = input() if key == "q": break
Q4
Scratch repeat until score = 10 becomes a while loop that uses not → while not score == 10: score = score + 1
Q5
while true: print("Go!") → True needs a capital T in Python. Writing true gives a NameError because Python does not know that name. Use while True:
Q6
while done: print("Working") → Scratch repeat until done means keep going until done is true, so the loop needs not. This loop would stop at the start while done is False. Use while not done:
Section C: Definitions
Q1
while True: — The Python version of the Scratch forever block. It repeats its indented code nonstop until break. Scratch repeat until x becomes while not x:
Section D: Fill in the Blanks
Q1
while True: print("Hello!")
Q2
while True: print("Go!")
Q3
done = False while not done: done = True
Q4
while True: if score >= 10: break
Q5
lives = 3 while not lives == 0: lives = lives - 1
Section E: Spot the Error
Q1
C — A double == compares values, so this line does not change score and the loop never reaches 5. Use a single = to assign: ' score = score + 1'.
Q2
C — An if statement must end with a colon. The line should be " if answer == 'yes':".
Q3
D — A single = assigns a value. To compare two values you need ==. The line should be ' if lives == 0:'.
Q4
D — The print() call is missing its closing bracket. It should be ' print(energy)'.
Q5
C — Scratch 'repeat until found' becomes 'while not found:'. With 'while found:' the loop never runs, because found starts as False.