Part 4: Examine items#

In this section we’ll add the examine command.

Part 4.1: Add new items#

In this section we’ll add a "desk" and "book" items to the ITEMS dictionary, which will eventually be added to the "home" place. The book is where we’ll find the hint about petting dragons.

We’ll also have to modify do_shop(), so that items without prices (like "book" and "desk") aren’t listed for sale.

Note: At the end of this section, there will be no difference in how the game behaves. But check and make sure that the book and desk are not listed when you do the shop command.

A. Modify ITEMS:#

  1. [ ] Add two items to the ITEMS dictionary with keys: "desk" and "book". Like previous items, each element one should be a dictionary with a "name" and "description"; unlike the others, these will have no "price".

Code
40ITEMS = {
41    "elixir": {
42        "key": "elixir",
43        "name": "healing elixir",
44        "description": "a magical elixir that will heal what ails ya",
45        "price": -10,
46    },
47    "dagger": {
48        "key": "dagger",
49        "name": "a dagger",
50        "description": "a 14 inch dagger with a double-edged blade",
51        "price": -25,
52    },
53    "desk": {
54        "key": "desk",
55        "name": "Desk",
56        "description": (
57            "A wooden desk with a large leather-bound book open on "
58            " its surface."
59        ),
60    },
61    "book": {
62        "key": "book",
63        "name": "A book",
64        "description": (
65            "A hefty leather-bound tome open to an interesting passage."
66        ),
67    },
68}

For now, keep the descriptions for both simple. Something like:

Desk

A wooden desk with a large leather-bound book open on its surface.

Book

A hefty leather-bound tome open to an interesting passage.

Note: If you try the shop command before the next section, you will see "book" and "desk" in the list.

B. Modify PLACES:#

We’ll keep track of which items are in a particular place by adding a "items" key to the place dictionary. In this case, we’re going to add the keys for the "book" and "desk" items to the "home" place.

  1. [ ] In the dictionary for "home" the key "items". The value should be a list that contains two items, the strings "book" and "desk".

Code
21PLACES = {
22    "home": {
23        "key": "home",
24        "name": "Your Cottage",
25        "east": "town-square",
26        "description": "A cozy stone cottage with a desk and a neatly made bed.",
27        "items": ["book", "desk"],
28    },

C. Modify do_shop(), in the for loop:#

  1. [ ] Before printing each item, check if the item has a "price" key. continue if not.

Code
105def do_shop():
106    """List the items for sale."""
107
108    header("Items for sale.")
109
110    for item in ITEMS.values():
111        if "price" not in item:
112            continue
113        write(f'{item["name"]:<13}  {item["description"]}')
114
115    print()
116

Be sure to test the shop command and make sure book and desk aren’t listed.

Part 4.2: Add do_examine()#

In this section we’ll add an examine command.

Demo

A. Define do_examine():#

  1. [ ] Add a function do_examine() with one parameter: args.

  2. [ ] Use the debug() function to print the value of args, something like:

    Trying to examine: args

Code
122def do_examine(args):
123    """Look at an item in the current place."""
124
125    debug(f"Trying to examine: {args}")
126
127

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

  1. [ ] Add an elif clause that checks if command is "x", "exam", or "examine".

    • If it is, call do_examine() and pass args.

Code
190        if command in ("q", "quit", "exit"):
191            do_quit()
192
193        elif command in ("shop"):
194            do_shop()
195
196        elif command in ("g", "go"):
197            do_go(args)
198
199        elif command in ("x", "exam", "examine"):
200            do_examine(args)
201
202        else:
203            error("No such command.")
204            continue
205
206        print()
207

Tip

This is a good time to test and make sure x, exam and examine all trigger calling the do_examine() function.

Part 4.3: Finish examine command#

In this section we’ll write the rest of the do_examine() function.

This will be very similar to the do_go() function. In that we’ll need to make sure the player typed something after the command, (in args) and that it is an item in the current place; then we’ll get the item from the ITEMS dictionary and print its information.

Demo

A. Modify do_examine(): ensure args is not empty#

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

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

    • [ ] return

Code
122def do_examine(args):
123    """Look at an item in the current place."""
124
125    debug(f"Trying to examine: {args}")
126
127    # make sure the player said what they want to examine
128    if not args:
129        error("What do you want to examine?")
130        return

B. Modify do_examine(): get the current 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

Code
122def do_examine(args):
123    """Look at an item in the current place."""
124
125    debug(f"Trying to examine: {args}")
126
127    # make sure the player said what they want to examine
128    if not args:
129        error("What do you want to examine?")
130        return
131
132    # look up where the player is now
133    place_name = PLAYER["place"]
134    place = PLACES[place_name]

C. Modify do_examine(): check the name#

  1. [ ] assign the first element from the args list to the variable name and make it lowercase

  2. [ ] check if name is in the list of items available at this place by:

    • [ ] use an if statement with the following condition:

      • [ ] check if name is not in the list returned in the next step

      • [ ] use the .get() method on place and pass it two arguments:

        • [ ] the key: "items"

        • [ ] the default value to return if missing: an empty list

    • [ ] if the above condition is met:

      • [ ] print an error message like: "Sorry, I don't know what this is: name."

      • [ ] return

  3. [ ] Check if name is a key in the ITEMS dictionary, if not:

    • [ ] Print an error message like:

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

      This will only happen if you made a mistake somewhere in your code. But just in case we do, we want to have a clear error message so we can tell what went wrong.

Code
122def do_examine(args):
123    """Look at an item in the current place."""
124
125    debug(f"Trying to examine: {args}")
126
127    # make sure the player said what they want to examine
128    if not args:
129        error("What do you want to examine?")
130        return
131
132    # look up where the player is now
133    place_name = PLAYER["place"]
134    place = PLACES[place_name]
135
136    # get the item entered by the user and make it lowercase
137    name = args[0].lower()
138
139    # make sure the item is in this place
140    if name not in place.get("items", []):
141        error(f"Sorry, I don't know what this is: {name!r}.")
142        return
143

D. Modify do_examine(): get and print the item info#

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

  2. [ ] Using the header() function print the item name

  3. [ ] Using the wrap() function print the item description

Code
122def do_examine(args):
123    """Look at an item in the current place."""
124
125    debug(f"Trying to examine: {args}")
126
127    # make sure the player said what they want to examine
128    if not args:
129        error("What do you want to examine?")
130        return
131
132    # look up where the player is now
133    place_name = PLAYER["place"]
134    place = PLACES[place_name]
135
136    # get the item entered by the user and make it lowercase
137    name = args[0].lower()
138
139    # make sure the item is in this place
140    if name not in place.get("items", []):
141        error(f"Sorry, I don't know what this is: {name!r}.")
142        return
143
144    # make sure the item is in the ITEMS dictionary
145    if name not in ITEMS:
146        error(f"Woops! The information about {name} seems to be missing.")
147        return
148
149    # get the item dictionary
150    item = ITEMS[name]
151
152    # print the item information
153    header(item["name"])
154    wrap(item["description"])
155