Part 6: Take things#

In this section we’ll add the take command.

Part 6.1: Add command#

In this section we’ll define a do_take() function that gets called when the player types t, take, or grab.

Demo

A: Define do_take()#

  1. [ ] Define a do_take() function.

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

Code
257def do_take(args):
258    """Pick up an item and add it to inventory."""
259    debug(f"Trying to take: {args}")
260

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

  1. [ ] Add an elif that checks if command is "t", "take" or "grab".

    • [ ] if so, call do_take()

Code
276        if command in ["q", "quit", "exit"]:
277            do_quit()
278
279        elif command in ("shop"):
280            do_shop()
281
282        elif command in ("g", "go"):
283            do_go(args)
284
285        elif command in ("x", "exam", "examine"):
286            do_examine(args)
287
288        elif command in ("l", "look"):
289            do_look()
290
291        elif command in ("t", "take", "grab"):
292            do_take(args)
293
294        else:
295            error("No such command.")
296            continue

Part 6.2: Validate item#

In this section we’ll check to make sure that the player entered a valid, takeable item in the current place.

Demo

A: Modify ITEMS#

  1. [ ] For any item you wish for the player to be able to take, add "can_take": True to the items dictionary.

Code
61    "book": {
62        "key": "book",
63        "can_take": True,
64        "name": "a book",
65        "description": (
66            "A hefty leather-bound tome open to an interesting passage."
67        ),
68    },

B: Modify do_take(): make sure the item is valid in the current place#

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

    • [ ] Use the error() function to print a message saying: "Which way do you want to go?"

    • [ ] return

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

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

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

  5. [ ] Get the list of items in the place dictionary using .get() with a default value of []. Check to see if name is in the list. If not:

    • [ ] Print an error message like:

      "Sorry, I don't see a name here."

    • [ ] return

Code
258def do_take(args):
259    """Pick up an item and add it to inventory."""
260    debug(f"Trying to take: {args}")
261
262    # make sure the player typed an item
263    if not args:
264        error("What do you want to take?")
265        return
266
267    # get the item name from arguments
268    # and make it lowercase
269    name = args[0].lower()
270
271    # look up where the player is now
272    place_name = PLAYER["place"]
273    place = PLACES[place_name]
274
275    # make sure the item is in this place
276    if name not in place.get("items", []):
277        error(f"Sorry, I don't see a {name!r} here.")
278        return
279

C: Modify do_take(): make sure the item is takeable#

  1. [ ] Using .get(), get the value from ITEMS associated with the name key and assign it to the variable item.

  2. [ ] If item is falsy,

    • [ ] Print an error message like:

      "Woops! The information about name!r seems to be missing."

    • [ ] return

  3. [ ] Using .get(), get the value from item associated with the "can_take" key. Check to see if it is falsy. If so:

    • [ ] Use wrap() to print a message like:

      "You try to pick up item['name'], but you find you aren't able to lift it."

    • [ ] return

Code
258def do_take(args):
259    """Pick up an item and add it to inventory."""
260    debug(f"Trying to take: {args}")
261
262    # make sure the player typed an item
263    if not args:
264        error("What do you want to take?")
265        return
266
267    # get the item name from arguments
268    # and make it lowercase
269    name = args[0].lower()
270
271    # look up where the player is now
272    place_name = PLAYER["place"]
273    place = PLACES[place_name]
274
275    # make sure the item is in this place
276    if name not in place.get("items", []):
277        error(f"Sorry, I don't see a {name!r} here.")
278        return
279
280    # get the item information
281    item = ITEMS.get(name)
282
283    # make sure the item is in the ITEMS dictionary
284    if not item:
285        error(f"Woops! The information about {name!r} seems to be missing.")
286        return
287
288    if not item.get("can_take"):
289        wrap(f"You try to pick up {item['name']}, but you find you aren't able to lift it.")
290        return
291

Part 6.3: Take it#

In this section we’ll actually take the item.

Demo

A: Modify PLAYER#

  1. [ ] Add "inventory": {} to the PLAYER dictionary.

Code
17PLAYER = {
18    "place": "home",
19    "inventory": {},

B: Modify do_take()#

In this section we will add the item to our inventory, remove it from the place, and let the player know that it’s done.

  1. [ ] Call .setdefault() on PLAYER["inventory"] with the arguments name and 0.

    This will set the current inventory for name to 0 if it is not already in our inventory.

  2. [ ] Add 1 to PLAYER["inventory"][name]

  3. [ ] Remove name from the place["items"] list using the .remove() method

  4. [ ] Use the wrap() function to print a message like:

    "You pick up name and put it in your pack."

Code
259def do_take(args):
260    """Pick up an item and add it to inventory."""
261    debug(f"Trying to take: {args}")
262
263    # make sure the player typed an item
264    if not args:
265        error("What do you want to take?")
266        return
267
268    # get the item name from arguments
269    # and make it lowercase
270    name = args[0].lower()
271
272    # look up where the player is now
273    place_name = PLAYER["place"]
274    place = PLACES[place_name]
275
276    # make sure the item is in this place
277    if name not in place.get("items", []):
278        error(f"Sorry, I don't see a {name!r} here.")
279        return
280
281    # get the item information
282    item = ITEMS.get(name)
283
284    # make sure the item is in the ITEMS dictionary
285    if not item:
286        error(f"Woops! The information about {name!r} seems to be missing.")
287        return
288
289    if not item.get("can_take"):
290        wrap(f"You try to pick up {item['name']}, but you find you aren't able to lift it.")
291        return
292
293    PLAYER["inventory"].setdefault(name, 0)
294    PLAYER["inventory"][name] += 1
295    place["items"].remove(name)
296
297    wrap(f"You pick up {item['name']} and put it in your pack.")

Part 6.4: Examine inventory#

In this section we’ll modify do_examine() so it can be used to look at inventory items.

Demo

A: Modify do_examine()#

  1. [ ] Find the if statement where you check if name is not in the place items list. Modify it so that it shows the error if name is not in place items and name is not in PLAYER["inventory"].

Code
178def do_examine(args):
179    """Look at an item in the current place."""
180
181    debug(f"Trying to examine: {args}")
182
183    # make sure the player said what they want to examine
184    if not args:
185        error("What do you want to examine?")
186        return
187
188    # look up where the player is now
189    place_name = PLAYER["place"]
190    place = PLACES[place_name]
191
192    # get the item entered by the user and make it lowercase
193    name = args[0].lower()
194
195    # make sure the item is in this place or in the players inventory
196    if not (name in place.get("items", []) or name in PLAYER["inventory"]):
197        error(f"Sorry, I don't know what this is: {name!r}.")
198        return
199