Python 07 · Pack a toolkit for the rescue

Python lesson 7 of 50 · Beginner · About 25 minutes

Pip has a torch and spanner but will need a fuse at the Beacon. Keep track of supplies with a list that can change.

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

Mission Create, inspect and edit a list and avoid adding a duplicate tool.

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.

Inspect. Read a tool by its position.. → Check. Look for the found tool in the list.. → Pack. Append a new tool and count the contents..
Inspect → Check → Pack

Understand the Python in this lesson

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

Put several values in a list

The square brackets in ["torch", "spanner"] make a list. Commas separate its items and quotes mark the text inside each item. The name toolkit holds the whole ordered collection.

Read an item by position

Square brackets after a list name select an item. In toolkit[0], zero is the first position. The index 1 means the second position. Counting positions from zero is different from counting how many items the list has.

Check for an empty reply or a duplicate

"" is an empty string, which a blank or spaces-only input becomes after cleaning. If the reply has text, found in toolkit asks whether that exact item is already present. The branch prints a useful message for each case.

Add to the existing list

toolkit.append(found) adds the new item to the end of the same list. The dot means call the list’s append method. The existing torch and spanner stay in their original positions.

Count and show the result

len(toolkit) returns the number of items, which the first final f-string puts in its message. Printing the whole list shows the items in order, with brackets and quotes to make the collection easy to inspect.

How the pieces fit Indexing reads a particular item. Membership checks whether something exists. Append changes the list, and len reports how many items are there afterward.

Follow one run, step by step
  1. Before typing, read toolkit[0].
    First tool: torch. Why The torch is at position zero in the original list.
  2. Type fuse.
    Added fuse. Why The reply is non-empty and fuse is absent, so the else block appends it.
  3. Read the two final reports.
    Tools packed: 3. Final toolkit: ['torch', 'spanner', 'fuse'] Why Length counts the updated list, then the next f-string displays it in its new order.

Predict before you run Predict the message and the list size if you run the file again and type TORCH.

Reveal the explanation

After cleaning, found holds "torch", which the starting list already holds. The duplicate message appears and the toolkit stays at two items.

Look up Python words and symbols in the course reference

Build the checkpoint

Python skills Lists, Index zero, append(), Membership with in.

1. Store the first two tools

Square brackets make a list. Put commas between the quoted tool names. Pip starts with a torch and a spanner. To read the first item, write toolkit[0]. Python counts positions from zero, so the second stored tool lives at position one.

toolkit = ["torch", "spanner"]
print(f"First tool: {toolkit[0]}.")

2. Look for a new tool

Ask the player to enter a tool from the Workshop. Clean the text in the same way as a radio command. Check empty text first so the rover cannot pack nothing. Then use found in toolkit to check whether the whole tool name already appears. A duplicate should leave the current list intact.

if found == "":
    print("No tool entered.")
elif found in toolkit:
    print(f"{found} is already packed.")

3. Add the tool and count the list

Use toolkit.append(found) in the remaining branch. It adds one item at the end of the list. Use len(toolkit) afterwards to count every item stored. Pip should carry three tools after finding a fuse and two tools after finding a spare torch. Restart between tests so the list starts fresh.

else:
    toolkit.append(found)
    print(f"Added {found}.")
Complete Python checkpoint · moonbase_07.py
toolkit = ["torch", "spanner"]
print(f"First tool: {toolkit[0]}.")
print(f"Packed: {toolkit}")

found = input("Tool from the Workshop: ").strip().lower()
if found == "":
    print("No tool entered.")
elif found in toolkit:
    print(f"{found} is already packed.")
else:
    toolkit.append(found)
    print(f"Added {found}.")

print(f"Tools packed: {len(toolkit)}.")
print(f"Final toolkit: {toolkit}")

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 toolkit = ["torch", "spanner"]
Work out the value on the right, then store it in toolkit.
Line 2 print(f"First tool: {toolkit[0]}.")
Show the message or value in the output. Python fills the braces with values before it uses the text. The brackets look up a dictionary key or a position that starts at zero.
Line 3 print(f"Packed: {toolkit}")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 5 found = input("Tool from the Workshop: ").strip().lower()
Work out the value on the right, then store it in found. 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 6 if found == "":
Run the indented block only if this check is true.
Line 7 print("No tool entered.")
Show the message or value in the output.
Line 8 elif found 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 9 print(f"{found} is already packed.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 10 else:
Take this branch if the earlier checks for this decision were false.
Line 11 toolkit.append(found)
Add the value to the end without replacing the earlier entries.
Line 12 print(f"Added {found}.")
Show the message or value in the output. Python fills the braces with values before it uses the text.
Line 14 print(f"Tools packed: {len(toolkit)}.")
Show the message or value in the output. Python fills the braces with values before it uses the text. Count the characters or items.
Line 15 print(f"Final toolkit: {toolkit}")
Show the message or value in the output. Python fills the braces with values before it uses the text.

Test the mission

  • Enter fuse and check that it appears at the end of the list.
  • Restart and enter TORCH. Check that there are still only two tools.
  • Try an empty line. The toolkit should contain exactly the two starting tools.

If something goes wrong

Python reports an index error.
The two starting tools have positions 0 and 1. A larger position does not exist yet.
My stored list disappears after append.
Use toolkit.append(found) as an instruction by itself. It changes the list where it is.

Mission extension

Have Pip report whether the fuse is in the final toolkit. Predict the response before you test both with and without adding it.