break to exit a loop

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

break to exit a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does the break keyword do inside a loop?
A It pauses the loop for one second
B It stops the loop straight away
2. Which loop runs forever unless something inside it uses break?
A while True:
B for i in range(3):
3. After break runs, which line does Python run next?
A The first line after the loop
B The first line inside the loop again
4. Which code uses break correctly?
A if guess == 7: break
B if guess == 7: break
5. A student writes this loop: while True: print("Hi") What happens?
A It prints Hi once and then stops
B It prints Hi forever because there is no break

Match the Pairs

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

Left columnRight column
1break AThere is no break, so the loop never ends and the program runs forever. Add an if statement with break inside the loop.
2while True: Bwhile True: break print('Done')
3Exit a loop when the user types 'quit' Cif answer == 'quit': break
4The line after the loop runs once break has ended the loop DStarts a loop that keeps running forever until a break is reached
5while True: name = input('Name: ') print(name) EThe break is not indented, so it is outside the loop and cannot end it. It must be indented inside the loop, usually under an if.
6while True: age = int(input('Age: ')) break FStops the loop it is in straight away and jumps to the first line after the loop

Definitions

break

Fill in the Blanks

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

1.
while True: if input("Quit? ") == "y": _____
2.
while ____: if input("Quit? ") == "y": break
3.
while True: __ input("Quit? ") == "y": break
4.
while True: if int(input("Guess: ")) __ 7: break
5.
_____ True: if input("Quit? ") == "y": break

Spot the Error

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

Question 1
A score = 0
B while true:
C score += 2
D if score >= 10:
E break
F print(score)
Question 2
A while True
B pw = input('Password: ')
C if pw == 'cat':
D break
E print('Welcome')
Question 3
A while True:
B n = int(input('Number: '))
C if n = 0:
D break
E print('Done')
Question 4
A while True:
B cmd = input('Go? ')
C if cmd == 'stop':
D brake
E print('Stopped')
Question 5
A while True:
B age = int(input('Age: '))
C if age > 0
D break
E print('Thanks')

Answer Key

break to exit a loop
Section A: Quick Questions
Q1
B — It stops the loop straight away
Q2
A — while True:
Q3
A — The first line after the loop
Q4
A — if guess == 7: break
Q5
B — It prints Hi forever because there is no break
Section B: Match the Pairs
Q1
break → Stops the loop it is in straight away and jumps to the first line after the loop
Q2
while True: → Starts a loop that keeps running forever until a break is reached
Q3
Exit a loop when the user types 'quit' → if answer == 'quit': break
Q4
The line after the loop runs once break has ended the loop → while True: break print('Done')
Q5
while True: name = input('Name: ') print(name) → There is no break, so the loop never ends and the program runs forever. Add an if statement with break inside the loop.
Q6
while True: age = int(input('Age: ')) break → The break is not indented, so it is outside the loop and cannot end it. It must be indented inside the loop, usually under an if.
Section C: Definitions
Q1
break — A keyword that stops the loop it is in straight away. The program then carries on with the first line after the loop. It is often used inside while True to exit when a condition is met.
Section D: Fill in the Blanks
Q1
while True: if input("Quit? ") == "y": break
Q2
while True: if input("Quit? ") == "y": break
Q3
while True: if input("Quit? ") == "y": break
Q4
while True: if int(input("Guess: ")) == 7: break
Q5
while True: if input("Quit? ") == "y": break
Section E: Spot the Error
Q1
B — True must start with a capital letter in Python. Lowercase true is not a keyword, so Python gives a NameError. It should be while True:
Q2
A — The while line is missing a colon at the end. Python needs the colon before the indented loop body. It should be while True:
Q3
C — A single = assigns a value. An if condition needs == to compare. It should be if n == 0: so the loop can break when the number is 0.
Q4
D — The keyword is spelled break, not brake. Python does not know brake, so the loop would not exit. It should be break.
Q5
C — The if line is missing its colon. Python needs a colon at the end of the line before the indented break. It should be if age > 0: