REPEAT ... UNTIL as while True with break

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

REPEAT ... UNTIL as while True with break

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Python has no REPEAT ... UNTIL. Which is the usual way to write one?
A while True: with an if ... break at the end of the body
B do: ... while condition, written at the end
2. Pseudocode: REPEAT ... UNTIL Guess = 7. Which Python test goes at the end of the body?
A if guess == 7: break
B if guess != 7: break
3. Where does the if ... break go in a while True loop that copies REPEAT ... UNTIL?
A At the start of the loop body
B At the end of the loop body
4. How many times does the body of a while True: loop (with the break test at the end) run?
A Zero times if the condition is already True
B At least once
5. What does break do inside a while True: loop?
A Leaves the loop straight away
B Skips to the next repeat of the loop

Match the Pairs

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

Left columnRight column
1while True: AThe condition is the wrong way round. UNTIL Num >= 1 should become if Num >= 1: break, but this stops on invalid input.
2if Guess == 7: break Bwhile True: Age = int(input()) if Age >= 0: break
3A REPEAT loop always runs its body at least once, because the test comes at the end. In Python the body goes straight after while True: CThe if and break are not indented, so they are outside the loop. The loop never stops and break causes an error.
4UNTIL Score >= 50 becomes an if test with the same condition, followed by break. The loop stops when this is True. DThe end-of-loop test that copies UNTIL Guess = 7. When the condition is True, the loop stops.
5while True: Num = int(input()) if Num > 0: break Eif Score >= 50: break
6while True: Num = int(input()) if Num < 1: break FStarts a loop that keeps repeating until a break stops it, so the body always runs at least once. This is Python's version of REPEAT.

Definitions

while True:

Fill in the Blanks

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

1.
while ____: name = input("Name: ") if name != "": break
2.
while True: pin = input("PIN: ") if pin == "1234": _____
3.
while True: score = int(input("Score: ")) if score __ 50: break
4.
while True: n = int(input("Enter 1-10: ")) __ 1 <= n <= 10: break
5.
_____ True: n = int(input("Number: ")) if n == 0: break

Spot the Error

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

Question 1
A while True:
B n = int(input())
C if n > 0
D break
Question 2
A while True:
B pw = input('Password: ')
C if pw = 'cat':
D break
Question 3
A total = 0
B while False:
C total += 1
D if total == 5:
E break
Question 4
A while true:
B x = int(input())
C if x >= 1 and x <= 10:
D break
Question 5
A while True:
B guess = input('Guess: '
C if guess == 'cat':
D break

Answer Key

REPEAT ... UNTIL as while True with break
Section A: Quick Questions
Q1
A — while True: with an if ... break at the end of the body
Q2
A — if guess == 7: break
Q3
B — At the end of the loop body
Q4
B — At least once
Q5
A — Leaves the loop straight away
Section B: Match the Pairs
Q1
while True: → Starts a loop that keeps repeating until a break stops it, so the body always runs at least once. This is Python's version of REPEAT.
Q2
if Guess == 7: break → The end-of-loop test that copies UNTIL Guess = 7. When the condition is True, the loop stops.
Q3
A REPEAT loop always runs its body at least once, because the test comes at the end. In Python the body goes straight after while True: → while True: Age = int(input()) if Age >= 0: break
Q4
UNTIL Score >= 50 becomes an if test with the same condition, followed by break. The loop stops when this is True. → if Score >= 50: break
Q5
while True: Num = int(input()) if Num > 0: break → The if and break are not indented, so they are outside the loop. The loop never stops and break causes an error.
Q6
while True: Num = int(input()) if Num < 1: break → The condition is the wrong way round. UNTIL Num >= 1 should become if Num >= 1: break, but this stops on invalid input.
Section C: Definitions
Q1
while True: — Python's version of REPEAT ... UNTIL: the body runs at least once, then an if with break at the end stops the loop when the UNTIL condition is True.
Section D: Fill in the Blanks
Q1
while True: name = input("Name: ") if name != "": break
Q2
while True: pin = input("PIN: ") if pin == "1234": break
Q3
while True: score = int(input("Score: ")) if score >= 50: break
Q4
while True: n = int(input("Enter 1-10: ")) if 1 <= n <= 10: break
Q5
while True: n = int(input("Number: ")) if n == 0: break
Section E: Spot the Error
Q1
C — An if statement must end with a colon. It should be 'if n > 0:'.
Q2
C — A single = assigns a value. The UNTIL test compares two values, so it needs ==: 'if pw == 'cat':'.
Q3
B — while False never runs the body, but a REPEAT loop must run at least once. It should be 'while True:'.
Q4
A — Python's Boolean value is True with a capital T. 'true' is an undefined name and causes an error. It should be 'while True:'.
Q5
B — The closing bracket of input() is missing, which causes a syntax error. It should be " guess = input('Guess: ')".