Part 7: Show inventory#

In this section we’ll add the inventory command.

Part 7.1: Add command#

In this section we’ll define a do_inventory() function that gets called when the player types i, or inventory.

Demo

A: Define do_inventory()#

  1. [ ] Define a do_inventory() function.

  2. [ ] In it, use the debug() function to print something like "Trying to show inventory.".

Code
299def do_inventory():
300    """Show the players inventory"""
301
302    debug("Trying to show inventory.")

B: Modify main(), in the while loop#

  1. [ ] Add an elif that checks if command is "i", or "inventory".

    • [ ] if so, call do_inventory()

Code
319        if command in ["q", "quit", "exit"]:
320            do_quit()
321
322        elif command in ("shop"):
323            do_shop()
324
325        elif command in ("g", "go"):
326            do_go(args)
327
328        elif command in ("x", "exam", "examine"):
329            do_examine(args)
330
331        elif command in ("l", "look"):
332            do_look()
333
334        elif command in ("t", "take", "grab"):
335            do_take(args)
336
337        elif command in ("i", "inventory"):
338            do_inventory()
339
340        else:
341            error("No such command.")
342            continue

Part 7.2: Print inventory#

In this section we’ll print the players inventory.

Demo

A: Modify do_inventory()#

  1. [ ] Use the header() function to print "Inventory"

  2. [ ] If PLAYER["inventory"] is falsy:

    • [ ] use the write() function to print "Empty."

  3. [ ] Iterate over the results of .items() on PLAYER["inventory"] using the variables name and qty.

    • [ ] Get the value associated with the name key from ITEMS and assign it to item.

    • [ ] Use the write() function to print the qty and item["name"]

  4. [ ] Print a blank line

Code
299def do_inventory():
300    """Show the players inventory"""
301
302    debug("Trying to show inventory.")
303
304    header("Inventory")
305
306    if not PLAYER["inventory"]:
307        write("Empty.")
308
309    for name, qty in PLAYER["inventory"].items():
310        item = ITEMS.get(name)
311        write(f"(x{qty:>2})  {item['name']}")
312
313    print()