Part 8: Drop things#

In this section we’ll add the drop command.

Part 8.1: Add command#

In this section we’ll define a do_drop() function that gets called when the player types drop.

Demo

A: Define do_drop()#

  1. [ ] Define a do_drop() function.

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

Code
315def do_drop(args):
316    """Remove an item from inventory"""
317
318    debug(f"Trying to drop: {args}.")

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

  1. [ ] Add an elif that checks if command is "drop".

    • [ ] if so, call do_drop() and pass args.

Code
335        if command in ("q", "quit", "exit"):
336            do_quit()
337
338        elif command in ("shop"):
339            do_shop()
340
341        elif command in ("g", "go"):
342            do_go(args)
343
344        elif command in ("x", "exam", "examine"):
345            do_examine(args)
346
347        elif command in ("l", "look"):
348            do_look()
349
350        elif command in ("t", "take", "grab"):
351            do_take(args)
352
353        elif command in ("i", "inventory"):
354            do_inventory()
355
356        elif command == "drop":
357            do_drop(args)
358
359        else:
360            error("No such command.")
361            continue

Part 8.2: Validate#

In this section we’ll check to make sure that the player entered an item they have in inventory.

Demo

A: Modify do_drop()#

  1. [ ] Check to see if args is falsy, if so:

    • [ ] Use the error() function to print a message saying:

      "What do you want to drop?"

    • [ ] return

  2. [ ] assign the first item of the args list to the variable name and make it lowercase

  3. [ ] Check if name is not in PLAYER["inventory"] or if PLAYER["inventory"][name] is falsy. If so:

    • [ ] Use the error() function to print a message saying:

      "You don't have any name."

    • [ ] return

Code
315def do_drop(args):
316    """Remove an item from inventory"""
317
318    debug(f"Trying to drop: {args}.")
319
320    # make sure the player typed an item
321    if not args:
322        error("What do you want to drop?")
323        return
324
325    # get the item name from arguments
326    # and make it lowercase
327    name = args[0].lower()
328
329    # make sure the item is in inventory
330    if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331        error(f"You don't have any {name!r}.")
332        return
333

Part 8.3: Drop it#

In this section we’ll check remove the item from the players inventory and add it to the place items.

Demo

A: Modify do_drop(): remove from inventory#

  1. [ ] subtract 1 from PLAYER["inventory"][name]

  2. [ ] remove item from inventory if the quantity is 0 by:

    if PLAYER["inventory"][name] is falsy:

    • [ ] call .pop() on PLAYER["inventory"] with the argument name

Code
315def do_drop(args):
316    """Remove an item from inventory"""
317
318    debug(f"Trying to drop: {args}.")
319
320    # make sure the player typed an item
321    if not args:
322        error("What do you want to drop?")
323        return
324
325    # get the item name from arguments
326    # and make it lowercase
327    name = args[0].lower()
328
329    # make sure the item is in inventory
330    if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331        error(f"You don't have any {name!r}.")
332        return
333
334    # remove from inventory
335    PLAYER["inventory"][name] -= 1
336    if not PLAYER["inventory"][name]:
337        PLAYER["inventory"].pop(name)
338

B: Modify do_drop(): add to place#

  1. [ ] get the value from PLAYER associated with the "place" key and assign it to place_name

  2. [ ] get the value from PLACES associated with place_name and assign it to place

  3. [ ] call .setdefault() on place with the argument "items" and []

  4. [ ] append name to place["items"]

  5. [ ] print a message using the wrap() function like: You set down the name.

Code
315def do_drop(args):
316    """Remove an item from inventory"""
317
318    debug(f"Trying to drop: {args}.")
319
320    # make sure the player typed an item
321    if not args:
322        error("What do you want to drop?")
323        return
324
325    # get the item name from arguments
326    # and make it lowercase
327    name = args[0].lower()
328
329    # make sure the item is in inventory
330    if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331        error(f"You don't have any {name!r}.")
332        return
333
334    # remove from inventory
335    PLAYER["inventory"][name] -= 1
336    if not PLAYER["inventory"][name]:
337        PLAYER["inventory"].pop(name)
338
339    # look up where the player is now
340    place_name = PLAYER["place"]
341    place = PLACES[place_name]
342
343    # add to place items
344    place.setdefault("items", [])
345    place["items"].append(name)
346
347    # print a message
348    wrap(f"You set down the {name}.")