Python 11 · Store Pip’s mission state

Python lesson 11 of 50 · Early intermediate · About 25 minutes

Pip has a room, a battery and a cargo hold. Put all three on one mission card that the program can read and update.

Pip sits at the crossing of moon tracks near the lit Workshop and Greenhouse, with a beacon on a far ridge.

Mission Use a dictionary to hold the facts that change while a game is running.

Get ready

This chapter starts with a fresh checkpoint of Pip’s mission. Compare it with your previous version to see how the game is organised for the next set of skills.

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.

Make a card. Put four starting facts in state.. → Read a label. Print the room and battery.. → Update one fact. Spend one battery unit on the scan..
Make a card → Read a label → Update one fact

Understand the Python in this lesson

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

Read the mission record

state = {…} makes a dictionary and gives it the name state. A dictionary stores values under labels called keys. The braces open and close the record. Inside, the colon links each key to its value and the commas mark the end of each pair.

Spot four kinds of value

"Habitat" is a string, or text. 8 is an integer, or whole number. [] starts an empty list ready to hold parts. False is a Boolean value, one of Python’s two truth values. Later, the game will check that flag to find out whether Pip has completed the rescue.

Choose a key to read

state["room"] asks for the value stored under the text key "room". The square brackets mean “look inside this dictionary”. Here print() receives two arguments. It shows the message and the looked-up value with a space between them.

Read first, then write

state["energy"] = state["energy"] - 1 reads and calculates the right side first. Python looks up 8, subtracts 1, then puts 7 back under the same key. Pip’s other mission values stay available for later instructions.

How the pieces fit Earlier lessons stored facts in individual variables. Pip can now pass one named record through the game, and each part of the mission can read or change the key it owns.

Follow one run, step by step
  1. Create state
    Room Habitat, energy 8, empty parts, rescued False. Why The four pairs give a fresh mission its starting values.
  2. Print the first battery message
    Battery units: 8 Why The first lookup reads energy before the scan.
  3. Run the scan update and print again
    After a practice scan: 7 Why The second lookup sees the newly stored number.
  4. Read the final flag
    Mission rescued? False Why The scan changed only the energy value.

Predict before you run If you start with "energy": 5, what will the two battery messages show?

Reveal the explanation

The first shows Battery units: 5 and the second shows After a practice scan: 4. The subtraction reads the starting value from the dictionary, so it works for either starting number.

Look up Python words and symbols in the course reference

Build the checkpoint

Python skills Dictionaries, Keys and values, Lists inside dictionaries.

1. Build a mission card

Start a fresh file for the next phase of Moonbase Rescue. A dictionary groups named pieces of information inside braces. Think of each key as a label on Pip’s console. The key room points to text, energy points to an integer, and parts starts as an empty list. The value False records that the rescue has yet to happen.

state = {
    "room": "Habitat",
    "energy": 8,
    "parts": [],
    "rescued": False,
}

2. Read one value

Type a key inside square brackets after state. The printed result tells you which fact Python found. Say the expression aloud as ‘the room in state’. Keep the key spelling the same everywhere it appears. Run the checkpoint and match every line with the part of the dictionary it reads.

print("Pip starts in", state["room"])
print("Battery units:", state["energy"])

3. Change one value at a time

Follow the assignment from right to left. Python reads the old battery amount, subtracts one and puts the new amount under the same key. The practice scan updates one fact. Pip still starts in the Habitat and the rescue is still incomplete. Run a second time and notice that the initial dictionary supplies a fresh starting amount.

state["energy"] = state["energy"] - 1
print("After a practice scan:", state["energy"])
print("Mission rescued?", state["rescued"])
Complete Python checkpoint · moonbase_11.py
state = {
    "room": "Habitat",
    "energy": 8,
    "parts": [],
    "rescued": False,
}

print("Pip starts in", state["room"])
print("Battery units:", state["energy"])
state["energy"] = state["energy"] - 1
print("After a practice scan:", state["energy"])
print("Mission rescued?", state["rescued"])

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–12
Lines 1–6 state = { "room": "Habitat", "energy": 8, "parts": [], "rescued": False, }
Work out the value on the right, then store it in state. Each colon pairs a key with its value.
Line 8 print("Pip starts in", state["room"])
Show the message or value in the output. The brackets look up a dictionary key or a position that starts at zero.
Line 9 print("Battery units:", state["energy"])
Show the message or value in the output. The brackets look up a dictionary key or a position that starts at zero.
Line 10 state["energy"] = state["energy"] - 1
Work out the value on the right, then store it in state["energy"]. The brackets look up a dictionary key or a position that starts at zero.
Line 11 print("After a practice scan:", state["energy"])
Show the message or value in the output. The brackets look up a dictionary key or a position that starts at zero.
Line 12 print("Mission rescued?", state["rescued"])
Show the message or value in the output. The brackets look up a dictionary key or a position that starts at zero.

Test the mission

  • Predict the battery number after the scan. Run the file to check.
  • Change the initial energy to 5, run once and look for 4 after the scan.

If something goes wrong

A key name raises KeyError.
Compare the spelling in square brackets with the dictionary. The two names must match, including capital letters.

Mission extension

Add a key called scans. Start it at 0 and make the practice scan increase it by one.