Part 4: Examine items
Contents
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
:#
[ ]
Add two items to theITEMS
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.
[ ]
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:#
[ ]
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()
:#
[ ]
Add a functiondo_examine()
with one parameter:args
.[ ]
Use thedebug()
function to print the value ofargs
, 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:#
[ ]
Add anelif
clause that checks ifcommand
is"x"
,"exam"
, or"examine"
.If it is, call
do_examine()
and passargs
.
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#
[ ]
Check to see ifargs
is falsy, if so:[ ]
Use theerror()
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#
[ ]
get the value fromPLAYER
associated with the"place"
key and assign it toplace_name
[ ]
get the value fromPLACES
associated withplace_name
and assign it toplace
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#
[ ]
assign the first element from theargs
list to the variablename
and make it lowercase[ ]
check ifname
is in the list of items available at this place by:[ ]
use anif
statement with the following condition:[ ]
check ifname
is not in the list returned in the next step[ ]
use the.get()
method onplace
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
[ ]
Check ifname
is a key in theITEMS
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#
[ ]
Get the value from theITEMS
dictionary associated with thename
key and assign it to the variableitem
[ ]
Using theheader()
function print the item name[ ]
Using thewrap()
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