Python 04 · Give Pip a radio command

Python lesson 4 of 50 · Beginner · About 20 minutes

The rover can broadcast a status report. Now let the player send a station name back and see Pip acknowledge it.

Pip reaches towards a radio and a coil inside the Habitat, with Earth visible through a round window.

Mission Read a station name with input and put it into a friendly radio report using an f-string.

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.

Prompt. Pip waits for a fictional station name.. → Store. The command becomes text in destination.. → Reply. An f-string puts the text into a status message..
Prompt → Store → Reply

Understand the Python in this lesson

Read the explanations, then follow the worked example before changing the code.

Start with what Pip knows

The variables rover and start already have values before the player types. The first report uses both, so the player knows who is listening and where the trip begins.

Ask for a value

input("Send a station name: ") first displays a prompt, then waits for the player to type and press Enter. The prompt describes what to type. The returned value is the text the player entered.

Store the returned text

The assignment destination = input(...) finishes when the player presses Enter. At that point destination holds the new string. This checkpoint keeps the spelling and capital letters as typed. The next lesson will make the program compare and clean them.

Put values into a sentence

The f right before the opening quote makes an f-string, which is a string with spaces for values. Inside it, {rover} means use the current value of rover. The curly braces mark the part Python evaluates. The rest of the sentence is ordinary text.

Fit two names into one route

In f"Route request: {start} to {destination}.", Python fills the two pairs of braces in order. The start still comes from the file, while the destination comes from the player. The full stop stays in the message.

How the pieces fit The program links a fixed start to a destination supplied by the player. The assignment carries that reply from the prompt to both output messages.

Follow one run, step by step
  1. Run to the first report.
    Pip is ready in the Habitat. Why The f-string inserts the values already set in rover and start.
  2. Type Workshop and press Enter.
    The string Workshop is assigned to destination. Why Input returns the player’s reply after the prompt.
  3. Read the two replies.
    Pip received: Workshop. Route request: Habitat to Workshop. Why The last two f-strings insert destination into the reply and the route report.

Predict before you run What would the last line show if you removed the f before its opening quote?

Reveal the explanation

It would show Route request: {start} to {destination}.. The braces stay visible in an ordinary string. Put the f back to fill the two spaces with values.

Look up Python words and symbols in the course reference

Build the checkpoint

Python skills input(), Text input, f-strings, Curly braces.

1. Ask for a station name

Place the instruction that reads a command after the greeting. The text inside input() is the prompt shown to the player. The program waits there until the player presses Enter. The returned text goes into destination. Type fictional station names like Workshop or Greenhouse when testing.

destination = input("Send a station name: ")

2. Give the answer a place in a sentence

Put f just before a string’s first quotation mark to create an f-string. Inside it, use curly braces around each variable name. Python replaces {rover} and {destination} with their current values when it makes the message. You can add punctuation or spaces around these placeholders.

print(f"{rover} received: {destination}.")

3. Send two different requests

Run the program and enter Workshop. Run it again and enter Greenhouse. Every new run starts in Habitat and waits for another station name. At this stage Pip simply repeats the message it received, so even a made-up station will appear. When we add decisions, Pip will learn which stations it can actually reach.

print(f"Route request: {start} to {destination}.")
Complete Python checkpoint · moonbase_04.py
rover = "Pip"
start = "Habitat"
print(f"{rover} is ready in the {start}.")
destination = input("Send a station name: ")
print(f"{rover} received: {destination}.")
print(f"Route request: {start} to {destination}.")

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–6
Line 1 rover = "Pip"
Work out the value on the right, then store it in rover.
Line 2 start = "Habitat"
Work out the value on the right, then store it in start.
Line 3 print(f"{rover} is ready in the {start}.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 4 destination = input("Send a station name: ")
Work out the value on the right, then store it in destination. Show the prompt and wait for the player to type a line. The answer starts as text.
Line 5 print(f"{rover} received: {destination}.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 6 print(f"Route request: {start} to {destination}.")
Show the message or value in the output. Python fills the braces with values before it uses the text.

Test the mission

  • Read the prompt, enter Workshop and press Enter once.
  • Try a blank input and notice where an empty value appears in the report.
  • Add an exclamation mark to the acknowledgement and run both routes again.

If something goes wrong

The program looks stuck.
It is probably waiting at input(). Click into the console, type a station and press Enter.
The output shows {destination}.
Add f immediately before the first quote of the string.

Mission extension

Ask separately for a mission task such as find the fuse. Include the station and the task in a single f-string.