Students keep an inventory in a list that grows with append() and shrinks with remove(), check with in before removing, list it with join(), and build a shopping list app.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 10 min
Read the unit primer first. bag.append(x) adds to the end; bag.remove(x) takes the first x out and stops with a ValueError if it is not there, so check with x in bag first. ", ".join(bag) joins a list of texts into one, with a comma and space between. items.sort() puts the list itself in order. Students met break and while True in the console bridge.
Checking with in before remove(). Let the ValueError happen first, then ask what the program should say when the thing isn't in the bag.
| What you will see | A question to ask | The fix |
|---|---|---|
| ValueError: list.remove(x): x not in list. | Was the item in the list? Did you check first? | if item in bag: bag.remove(item), with an else that replies. |
| TypeError when joining the list. | Is everything in the list text? | Only lists of text can be joined; store words, or use str() first. |
| The list prints as ['lamp', 'key'] with brackets and quotes. | How do you want it to look? | Use ", ".join(bag) to print lamp, key. |
One possible solution for each task. Other correct programs are fine.
bag = []
while True:
command = input("> ").strip().lower()
words = command.split()
if command == "quit":
break
elif command == "bag":
print("You carry: " + ", ".join(bag))
print("That is " + str(len(bag)) + " things.")
elif len(words) == 2 and words[0] == "take":
bag.append(words[1])
print("You take the " + words[1] + ".")
elif len(words) == 2 and words[0] == "drop":
if words[1] in bag:
bag.remove(words[1])
print("You drop the " + words[1] + ".")
else:
print("You don't have a " + words[1] + ".")
else:
print("Try: take lamp, drop lamp, bag or quit")
bag = []
while True:
command = input("> ").strip().lower()
words = command.split()
if command == "quit":
break
elif command == "bag":
print("You carry: " + ", ".join(bag))
elif len(words) == 2 and words[0] == "take":
if len(bag) < 3:
bag.append(words[1])
print("You take the " + words[1] + ".")
else:
print("Your bag is full.")
elif len(words) == 2 and words[0] == "drop":
if words[1] in bag:
bag.remove(words[1])
print("You drop the " + words[1] + ".")
else:
print("You don't have a " + words[1] + ".")
else:
print("Try: take lamp, drop lamp, bag or quit")
items = []
while True:
command = input("add, remove, show or done: ").strip().lower()
if command == "done":
break
elif command == "add":
items.append(input("What? ").strip().lower())
elif command == "remove":
item = input("What? ").strip().lower()
if item in items:
items.remove(item)
else:
print(item + " is not on the list.")
elif command == "show":
items.sort()
print(", ".join(items))
print("Final list: " + ", ".join(items))
Stretch: Let the shopping list remember how many of each item (milk x2), ready for next lesson's dictionaries.
Support: Give students the loop and the done command, and ask them to add add, remove and show one at a time.
bag = ["lamp"]. What happens with bag.remove("key"), and how do you avoid it?
It stops with a ValueError because key is not in the bag. Check first: if "key" in bag: bag.remove("key").
Next lesson stores rooms and exits in dictionaries. The bag comes back in the text adventure.