Python 10 · Launch the first Moonbase Rescue mission

Python lesson 10 of 50 · Beginner · About 40 minutes

Put the radio, battery, tools and routes together. Guide Pip to the Workshop, collect a fuse and restore the Beacon before the rover runs low on energy.

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

Mission Complete a replayable text mission with a route choice, energy limit, collecting rule and visible win condition.

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.

Prepare. Pip starts at Habitat with eight units and a toolkit.. → Recover. Travel to the Workshop and collect the fuse.. → Restore. Save enough energy to reach and repair the Beacon..
Prepare → Recover → Restore

Understand the Python in this lesson

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

Build the starting state

The station, energy and toolkit variables hold the current state of Pip’s mission. Set all three before the loop begins. Inside the loop, the status f-string reports their latest state before asking the player for another command.

Handle commands in order

The checks at the top handle quit and charge before the travel instructions. break leaves the loop. continue skips the rest of this turn and goes back to the status and prompt. Charging restores eight units and puts Pip in Habitat, while leaving the toolkit alone.

Block journeys before spending energy

not in checks that a command is outside the accepted list. A second check uses and because it should block only when both facts are true, the destination is Beacon and a fuse is missing. If the first comparison is false, Python can skip checking the toolkit.

Choose a cost on one line

cost = 3 if command == "beacon" else 2 makes a value choice. It stores three for the Beacon and two for the other accepted destinations. Python checks the battery against that cost. It calls travel and stores the remaining energy only when the trip can go ahead.

Update the station and its story

command.title() changes text like "workshop" to "Workshop" for display and the station checks. An indented check inside the Workshop branch adds a fuse only if one is absent. The Greenhouse shows a clue. The Beacon removes the packed fuse with remove() and ends the loop after announcing success.

How the pieces fit Each turn follows the same order. Read the command, check whether the action is allowed, update the mission state, tell the player what changed, then repeat or end.

Follow one run, step by step
  1. From the starting state, try beacon.
    The Beacon needs a fuse from the Workshop. Why The inventory check blocks the route before adding costs or changing station, so energy stays at 8.
  2. Type workshop.
    Fuse added to toolkit. The next status shows Workshop with energy 6. Why The journey costs two units and the Workshop block appends one fuse.
  3. Type beacon again.
    Beacon restored! Pip has 3 energy left. Why Pip has the fuse and can pay three units. The Beacon block uses the fuse and exits the loop.

Predict before you run What happens if you type workshop twice before going to the Beacon? How many fuses are packed and how much energy remains on arrival at the Beacon?

Reveal the explanation

The two Workshop trips each cost two units, and the second visit says A fuse is already packed. There is still just one fuse. The Beacon costs three more units, so Pip arrives with 1, then uses the fuse.

Look up Python words and symbols in the course reference

Build the checkpoint

Python skills State, Functions in a game loop, Lists as inventory, Victory and restart tests.

1. Start a fresh mission

Keep the travel function above the mission. Set the station, energy and toolkit once before the loop, then print the first status card. Together these values are the game state: the information that describes the current moment. Keeping the starting values outside the loop lets Pip’s progress carry from one radio command to the next.

rover = "Pip"
station = "Habitat"
energy = 8
toolkit = ["torch", "spanner"]

2. Check the command before spending energy

Handle quit and charge first. Charging returns Pip to Habitat and fills the battery. Use continue to go back to the start of the loop after a blocked route or a charge. Check that the player has a fuse before attempting the Beacon. Visit the Workshop or Greenhouse for two units and the Beacon for three.

if command == "beacon" and "fuse" not in toolkit:
    print("The Beacon needs a fuse from the Workshop.")
    continue

cost = 3 if command == "beacon" else 2

3. Collect a part and send the winning signal

A Workshop visit adds a fuse if one is missing. A Greenhouse visit prints a clue. The remaining branch happens at the Beacon after the required checks and a successful journey. Remove the fuse to show Pip installed it, print the victory message and use break to end the mission. Test a short route first, then try a detour and recovery.

toolkit.remove("fuse")
print(f"Beacon restored! {rover} has {energy} energy left.")
break
Complete Python checkpoint · moonbase_10.py
def travel(energy, cost):
    if energy < cost:
        return energy
    return energy - cost


rover = "Pip"
station = "Habitat"
energy = 8
toolkit = ["torch", "spanner"]

print(f"Mission control to {rover}: restore the Beacon.")
while True:
    print(f"Station: {station} | Energy: {energy}/8")
    command = input("Workshop, Greenhouse, Beacon, charge or quit? ").strip().lower()

    if command == "quit":
        print("Mission paused. Click Run to start again.")
        break

    if command == "charge":
        station = "Habitat"
        energy = 8
        print("Recharged at Habitat.")
        continue

    if command not in ["workshop", "greenhouse", "beacon"]:
        print("Unknown command. Try one of the menu choices.")
        continue

    if command == "beacon" and "fuse" not in toolkit:
        print("The Beacon needs a fuse from the Workshop.")
        continue

    cost = 3 if command == "beacon" else 2
    if energy < cost:
        print("Not enough energy. Enter charge to recover at Habitat.")
        continue

    energy = travel(energy, cost)
    station = command.title()

    if station == "Workshop":
        if "fuse" not in toolkit:
            toolkit.append("fuse")
            print("Fuse added to toolkit.")
        else:
            print("A fuse is already packed.")
    elif station == "Greenhouse":
        print("A note says: bring the Workshop fuse to the Beacon.")
    else:
        toolkit.remove("fuse")
        print(f"Beacon restored! {rover} has {energy} energy left.")
        break

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–17
Line 1 def travel(energy, cost):
Define travel, a reusable piece of the program. Its input names are energy, cost. The indented body runs when something calls it.
Line 2 if energy < cost:
Run the indented block only if this check is true.
Line 3 return energy
Leave this function now and hand the result back to the line that called it.
Line 4 return energy - cost
Leave this function now and hand the result back to the line that called it.
Line 7 rover = "Pip"
Work out the value on the right, then store it in rover.
Line 8 station = "Habitat"
Work out the value on the right, then store it in station.
Line 9 energy = 8
Work out the value on the right, then store it in energy.
Line 10 toolkit = ["torch", "spanner"]
Work out the value on the right, then store it in toolkit.
Line 12 print(f"Mission control to {rover}: restore the Beacon.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 13 while True:
Repeat the indented work until a break, return or error ends this path.
Line 14 print(f"Station: {station} | Energy: {energy}/8")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 15 command = input("Workshop, Greenhouse, Beacon, charge or quit? ").strip().lower()
Work out the value on the right, then store it in command. 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 17 if command == "quit":
Run the indented block only if this check is true.
Lines 18–33
Line 18 print("Mission paused. Click Run to start again.")
Show the message or value in the output.
Line 19 break
Leave the nearest loop immediately and continue after it.
Line 21 if command == "charge":
Run the indented block only if this check is true.
Line 22 station = "Habitat"
Work out the value on the right, then store it in station.
Line 23 energy = 8
Work out the value on the right, then store it in energy.
Line 24 print("Recharged at Habitat.")
Show the message or value in the output.
Line 25 continue
Skip the rest of this pass and start the next loop check.
Line 27 if command not in ["workshop", "greenhouse", "beacon"]:
Run the indented block only if this check is true. Membership checks ask whether an item or key is in a collection.
Line 28 print("Unknown command. Try one of the menu choices.")
Show the message or value in the output.
Line 29 continue
Skip the rest of this pass and start the next loop check.
Line 31 if command == "beacon" and "fuse" not in toolkit:
Run the indented block only if this check is true. With and, every check must be true. Membership checks ask whether an item or key is in a collection.
Line 32 print("The Beacon needs a fuse from the Workshop.")
Show the message or value in the output.
Line 33 continue
Skip the rest of this pass and start the next loop check.
Lines 35–49
Line 35 cost = 3 if command == "beacon" else 2
Work out the value on the right, then store it in cost. Choose the first value if the condition is true, otherwise choose the value after else.
Line 36 if energy < cost:
Run the indented block only if this check is true.
Line 37 print("Not enough energy. Enter charge to recover at Habitat.")
Show the message or value in the output.
Line 38 continue
Skip the rest of this pass and start the next loop check.
Line 40 energy = travel(energy, cost)
Work out the value on the right, then store it in energy. Run travel with the supplied inputs and come back here when it returns.
Line 41 station = command.title()
Work out the value on the right, then store it in station. Return words with title-style capital letters for display.
Line 43 if station == "Workshop":
Run the indented block only if this check is true.
Line 44 if "fuse" not in toolkit:
Run the indented block only if this check is true. Membership checks ask whether an item or key is in a collection.
Line 45 toolkit.append("fuse")
Add the value to the end without replacing the earlier entries.
Line 46 print("Fuse added to toolkit.")
Show the message or value in the output.
Line 47 else:
Take this branch if the earlier checks for this decision were false.
Line 48 print("A fuse is already packed.")
Show the message or value in the output.
Line 49 elif station == "Greenhouse":
Run the indented block only if this check is true.
Lines 50–54
Line 50 print("A note says: bring the Workshop fuse to the Beacon.")
Show the message or value in the output.
Line 51 else:
Take this branch if the earlier checks for this decision were false.
Line 52 toolkit.remove("fuse")
Remove the matching item from the collection.
Line 53 print(f"Beacon restored! {rover} has {energy} energy left.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 54 break
Leave the nearest loop immediately and continue after it.

Test the mission

  • Win with Workshop, then Beacon. Compare the remaining battery with your calculation.
  • Send Beacon first and check that the rover stays in Habitat with eight units.
  • Try extra Greenhouse trips, run low, enter charge and then return to the Beacon. Check that the fuse is still packed.

If something goes wrong

The fuse disappears after a new command.
Keep the starting toolkit above the loop so each turn can use the list from the last turn.
The wrong route uses energy.
Put the command, fuse and energy checks before the line that calls travel.
The rover keeps asking after restoring the Beacon.
Put break inside the victory branch after the final message.

Mission extension

Add a second repair item called wire. Choose which station holds it, then extend the repair conditions and test a win, a missing item and a recharge.