Python 05 · Choose between two stations

Python lesson 5 of 50 · Beginner · About 25 minutes

The Workshop holds repair gear and the Greenhouse holds clues. Pip must recognise either destination and remain at Habitat when a command is unfamiliar.

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

Mission Use if, elif and else to decide which station report the rover should print.

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.

Clean. Format the radio input for comparison.. → Decide. Choose exactly one matching branch.. → Report. Show the station name from the resulting state..
Clean → Decide → Report

Understand the Python in this lesson

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

Clean the radio command one step at a time

Python reads the chain input(...).strip().lower() from left to right. input gets the reply. The dot calls strip() on that returned string to trim whitespace at both edges. A second dot calls lower() on the trimmed string. The cleaned reply goes into choice.

Ask a yes-or-no question

choice == "workshop" compares two values. Its answer is either true or false. Two equals signs compare, while one equals sign assigns a new value. The command has already been changed to lowercase, which makes "workshop" a useful comparison.

Write the first branch

A colon at the end of an if line introduces a block. Indent the next instructions, usually with four spaces for this level. Then Python knows which assignment and message belong to the Workshop choice.

Try another branch if needed

elif means else if. Python checks the Greenhouse condition only if the Workshop condition was false. else catches every remaining reply. Only one branch of this chain runs.

Report after the branches join

The final print starts at the left margin. It runs after whichever branch ran. A good choice changes station inside its branch. An unknown choice keeps the Habitat value from the first line.

How the pieces fit Clean the input, compare it, run one matching block, then use the updated value in a report shared by all choices.

Follow one run, step by step
  1. Type WORKSHOP with spaces on both sides.
    The value in choice is workshop. Why Strip removes the edge spaces and lower changes the capital letters.
  2. Check the Workshop condition.
    You enter the Workshop. A tool rack glints. Why The comparison is true, so Python stores Workshop in station and prints that branch’s message.
  3. Read the unindented report.
    Current station: Workshop. Why The program’s shared last step uses the station set in the selected branch.

Predict before you run What happens if the player types crater, and which instruction decides the final station?

Reveal the explanation

Both comparisons are false, so else prints the unknown-destination message. The first line set station to "Habitat", and the else branch leaves it there.

Look up Python words and symbols in the course reference

Build the checkpoint

Python skills if, elif and else, Comparison with ==, Indentation, Normalising input.

1. Clean up the command

People may type capital letters or leave a space after a word. The string method .strip() removes spaces at the beginning and end. The method .lower() changes letters to lowercase. Apply them in that order so inputs such as Workshop and workshop can reach the same branch.

choice = input("Destination: ").strip().lower()

2. Give Pip two known routes

Use if to ask whether the cleaned command equals "workshop". Compare with two equals signs. Under the condition, indent the lines that belong to it by four spaces. Next add elif, short for “else if”, to check Greenhouse when the first condition does not match.

if choice == "workshop":
    station = "Workshop"
    print("You enter the Workshop. A tool rack glints.")
elif choice == "greenhouse":
    station = "Greenhouse"
    print("You enter the Greenhouse. A plant light blinks.")

3. Keep a safe starting station

Give every unknown command a useful fallback with else. The program starts with station set to Habitat, so the fallback can leave it alone. The final report runs outside the branches. Align that line with if so it runs whether the player chooses a known route or an unknown one.

else:
    print("Unknown destination. Stay in Habitat.")

print(f"Current station: {station}.")
Complete Python checkpoint · moonbase_05.py
station = "Habitat"
print("Pip is at Habitat. Try workshop or greenhouse.")
choice = input("Destination: ").strip().lower()

if choice == "workshop":
    station = "Workshop"
    print("You enter the Workshop. A tool rack glints.")
elif choice == "greenhouse":
    station = "Greenhouse"
    print("You enter the Greenhouse. A plant light blinks.")
else:
    print("Unknown destination. Stay in Habitat.")

print(f"Current 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–14
Line 1 station = "Habitat"
Work out the value on the right, then store it in station.
Line 2 print("Pip is at Habitat. Try workshop or greenhouse.")
Show the message or value in the output.
Line 3 choice = input("Destination: ").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 print("You enter the Workshop. A tool rack glints.")
Show the message or value in the output.
Line 8 elif 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 print("You enter the Greenhouse. A plant light blinks.")
Show the message or value in the output.
Line 11 else:
Take this branch if the earlier checks for this decision were false.
Line 12 print("Unknown destination. Stay in Habitat.")
Show the message or value in the output.
Line 14 print(f"Current station: {station}.")
Show the message or value in the output. Python fills the braces with values before it uses the text.

Test the mission

  • Run all three routes, including an input of crater.
  • Try workshop in capital letters with a trailing space. It should still open the Workshop route.
  • For every route, look for the final station report.

If something goes wrong

Python reports a syntax error at the condition.
Write == to compare values and add a colon at the end of each branch header.
The final report only appears on one route.
Move its indentation back to the left edge so it sits outside the branches.

Mission extension

Add a Beacon branch before the fallback. Pip can reach it, but the narrator should say that it still needs a repair part.