Python lesson 6 of 50 · Beginner · About 25 minutes
A noisy transmission can reach the rover with only part of a word. Have Pip ask for a new command until it receives a destination or the player quits.

Mission Use a while loop, input validation and break to react to repeated radio commands.
Get ready
Read this as the next checkpoint in Pip’s mission. You can also start with the complete program below.
Open the browser Python editor (opens in a new tab), replace its example with the complete checkpoint below and choose Run. Use Python 3.10 or newer.

Understand the Python in this lesson
Read the explanations, then follow the worked example before changing the code.
Repeat a group of instructions
while keeps running a block while its condition is true. Here the condition is True, Python’s built-in true value. The loop will keep listening until a statement inside tells it to stop.
Ask again inside the loop
The input instruction is indented under while True:. That placement makes it ask for a fresh command on each turn. It cleans each reply with strip() and lower() before the comparisons.
Read two levels of indentation
Each if check is inside the loop. Its matching instructions are indented one level farther. In the Workshop block, both the station assignment and break belong to the Workshop check.
Break out when the answer is clear
break ends the nearest loop immediately. A recognised station sets station before the break. A recognised quit prints the ending message and then breaks, so Pip keeps the original station.
Find the retry and the final report
The retry message lines up with the if checks, so it stays inside the loop. It runs if none of the checks broke out. The final report starts at the left margin, so Python reaches it only after leaving the loop.
How the pieces fit Indentation makes the prompt repeat. The checks and break provide exits. The final unindented line reports what happened after one or more attempts.
-
Type workshp and press Enter.
Signal unclear. Try workshop, greenhouse or quit. Why The typo matches none of the three checks, so the retry message is reached. -
At the new prompt, type GREENHOUSE with some edge spaces.
The Greenhouse branch updates station and exits the loop. Why The prompt is inside the loop, and the cleaned command now matches a recognised destination. -
Read the line after the loop.
Final station: Greenhouse. Why Break moves execution to the next instruction outside the loop.
Predict before you run What happens if you press Enter with a blank reply and then type quit?
Reveal the explanation
A blank reply matches none of the checks, so Pip asks again. Then quit ends the loop. The final station is Habitat because neither station assignment ran.
Build the checkpoint
Python skills while True, break, Validation, Loop exit conditions.
1. Put the prompt inside a loop
The rover needs to hear a new transmission after each invalid one. Put input() inside while True:. This loops until an instruction asks it to stop. Indent everything that belongs to the loop by four spaces, including the prompt and the decisions that check the input.
while True:
choice = input("Workshop, Greenhouse or quit? ").strip().lower()
2. End the loop when a destination is clear
When the command is Workshop or Greenhouse, update the station and use break to leave the loop. In each branch the station changes before the break. The final report sits after the loop, so it will show the station that was selected on the valid turn.
if choice == "workshop":
station = "Workshop"
break
3. Give an unclear signal another turn
The retry message sits at the end of the loop body. An unfamiliar command reaches that line and then the loop runs again from the prompt. A quit branch gives the player a deliberate way to leave, even after several typos. Check that quit prints its own message and preserves the starting station.
if choice == "quit":
print("Radio session ended.")
break
print("Signal unclear. Try workshop, greenhouse or quit.")
Complete Python checkpoint · moonbase_06.py
station = "Habitat"
while True:
choice = input("Workshop, Greenhouse or quit? ").strip().lower()
if choice == "workshop":
station = "Workshop"
break
if choice == "greenhouse":
station = "Greenhouse"
break
if choice == "quit":
print("Radio session ended.")
break
print("Signal unclear. Try workshop, greenhouse or quit.")
print(f"Final station: {station}.")Read the complete checkpoint line by line
Use this guide beside the checkpoint. Blank lines separate ideas. Indented lines belong to the instruction above them. Lines with brackets still open can carry on below.
Lines 1–15
-
Line 1
station = "Habitat" - Work out the value on the right, then store it in station.
-
Line 2
while True: - Repeat the indented work until a break, return or error ends this path.
-
Line 3
choice = input("Workshop, Greenhouse or quit? ").strip().lower() - Work out the value on the right, then store it in choice. Show the prompt and wait for the player to type a line. The answer starts as text. Return a copy with whitespace removed from the beginning and end. Return a lowercase copy so capital letters do not change a command.
-
Line 5
if choice == "workshop": - Run the indented block only if this check is true.
-
Line 6
station = "Workshop" - Work out the value on the right, then store it in station.
-
Line 7
break - Leave the nearest loop immediately and continue after it.
-
Line 8
if choice == "greenhouse": - Run the indented block only if this check is true.
-
Line 9
station = "Greenhouse" - Work out the value on the right, then store it in station.
-
Line 10
break - Leave the nearest loop immediately and continue after it.
-
Line 11
if choice == "quit": - Run the indented block only if this check is true.
-
Line 12
print("Radio session ended.") - Show the message or value in the output.
-
Line 13
break - Leave the nearest loop immediately and continue after it.
-
Line 15
print("Signal unclear. Try workshop, greenhouse or quit.") - Show the message or value in the output.
Lines 17–17
-
Line 17
print(f"Final station: {station}.") - Show the message or value in the output. Python fills the braces with values before it uses the text.
Test the mission
- Enter workshp, then greenhouse. Check that the first command gets a retry and the second finishes the mission.
- Restart and enter quit. The final station should still be Habitat.
- Try two unfamiliar names before entering a real station. Count how often the retry message appears.
If something goes wrong
- The program asks once and never retries.
- Indent the prompt and the retry message to put both inside the loop.
- A valid station keeps looping.
- Check that break is indented inside the matching condition.
Mission extension
Count unclear signals with a variable. Print the count when the player reaches a station or quits.