← Back to course
Code Crew G6+ / Week 04 / Thursday
4/6
Week 04 Β· Loops

Thursday

Leaving early with break
// for, while, break and a countdown clock
⏱ about 30 min

Thursday: Leaving early with break

The docking bay game is the crew's favorite break-time puzzle. One person thinks of a bay number from 1 to 10, and the other guesses until they find it.

"I want the computer to run it," Rocket says. "But I cannot know how many guesses it will take. Could be one, could be ten."

Raven thinks about yesterday. "What do you notice?" she asks. "The loop should stop the moment the guess is right, wherever that happens inside the loop."

Nova hovers in a small circle and then darts straight out of it. "Would you like a hint?" she asks. "A loop can run forever on purpose, as long as it has a way out. That way out is called break."

Rocket writes while True and smiles.

while True and break

while True: starts a loop whose condition is always True. On its own it would never stop. The break statement jumps out of the loop at once, and the program carries on after it.

This pattern is perfect for asking again and again until the user gives the answer you need.

# ready_loop.py
while True:
    answer = input("Type go to launch: ").strip().lower()
    if answer == "go":
        break
    print("Holding...")
print("Launching!")
Type go to launch: wait
Holding...
Type go to launch: not yet
Holding...
Type go to launch: GO
Launching!
FOLLOW THE LOOP
  • Read the question.
  • Tap your answer.
When the user types GO, why is Holding... not printed?

The docking bay game

# docking.py
secret = 7
tries = 0
while True:
    guess = int(input("Guess the docking bay (1 to 10): "))
    tries = tries + 1
    if guess == secret:
        break
    elif guess < secret:
        print("Higher")
    else:
        print("Lower")
print(f"Docked in {tries} tries!")
Guess the docking bay (1 to 10): 5
Higher
Guess the docking bay (1 to 10): 9
Lower
Guess the docking bay (1 to 10): 7
Docked in 3 tries!
The user guesses 2, then 7. What number does the last line show for tries?
The user guesses 10. What word does the program print?
At your computer
1. Type ready_loop.py, save it and run it. Type wait, then not yet, then GO.
2. Type docking.py, save it and run it. Guess until you dock.
3. Change the secret to another number. Ask a grown-up to play.
4. Challenge: add a line so the game also prints Bay found! just before the break.
Next week you will learn how to make the computer pick the secret by itself.

Practice: three tries and a way out

A loop can have more than one break. This one leaves when the launch word is right, or when the user has used three tries.

# three_tries.py
tries = 0
while True:
    word = input("Launch word: ").strip().lower()
    tries = tries + 1
    if word == "nova":
        print("Launch word accepted")
        break
    if tries == 3:
        print("Three tries used. Take a break and try again later.")
        break
    print("Not that one. Try again.")
Launch word: orbit
Not that one. Try again.
Launch word: comet
Not that one. Try again.
Launch word: moon
Three tries used. Take a break and try again later.
FOLLOW THE EXITS
  • Read the question.
  • Tap your answer.
The user types Nova first. How many tries are used?
Why does Not that one show only twice in the second run?
What makes this loop stop without a right answer?
The user types moon, then NOVA. How many times does Not that one appear?
Which number would you change to allow five tries?
StatementTrue or false?
A loop can have more than one break.?
break leaves only the if, not the loop.?
while True runs forever unless something breaks out.?
Lines after break in the same block still run.?
WHY THIS EXERCISEbreak ends the loop at once, so nothing after it in that round runs.

while True with break is a tool you will use in the Mission Log menu later in the course. Tomorrow you will build a real countdown clock.

← Wednesday