Add to a list with append(), take out with remove(), check with in, and list it with join().
A shopping basket, a playlist, a to-do list: all lists that grow and shrink while the program runs.
The adventurer needs a bag: a list that starts empty, grows when something is taken and shrinks when it is dropped. Lists have their own commands for that, called with a dot, like text.
Make your prediction first. Help opens once you've locked it in.
bag = ["lamp", "map"]
bag.append("key")
bag.remove("map")
print(bag)
print("map" in bag)
bag.append("key") adds to the end of the list; bag.remove("map") takes that item out. What does the bag hold at the end?
Run it and try: take lamp, take key, bag, drop map, drop lamp, bag, quit. The loop keeps asking until quit (break leaves it, as in the console bridge).
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":
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":
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")
Count the bag after each command.
The line before remove().
A drop that crashes.
Start with an empty bag. Fill in how many things are in it after each command.
| command | things in the bag |
|---|---|
| take lamp | 1 |
| take key | |
| drop map | |
| drop lamp | |
| take rope |
You type map and it stops with ValueError: list.remove(x): x not in list. Click the line where it stops.
When the command is bag, also print how many things are in it.
len(bag) is how many things are in it. Add a line to the bag command that prints it.
The bag holds at most 3 things. Taking a fourth prints Your bag is full.
Before bag.append(...), check if len(bag) < 3:. Otherwise print that the bag is full.
When the command is bag, also say how many things are in it.
Run your program. This ticks when your code does all of the above.
The bag holds at most 3 things.
Run your program. This ticks when your code does all of the above.
Write a shopping list app. It keeps asking add, remove, show or done. add asks what to add; remove asks what to remove (and says so if it is not on the list); show prints the list in order (items.sort() puts it in order); done prints the final list and stops.
A shopping list: add, remove (checked with in), show in order, until done.
Run your program. This ticks when your code does all of the above.
Next lesson adds the map: rooms and exits stored in dictionaries, so the adventurer can walk from room to room.
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. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
Next lesson stores rooms and exits in dictionaries. The bag comes back in the text adventure.