Part 5: Look around
Contents
Part 5: Look around#
In this section we’ll add the look command.
Part 5.1: Add command#
In this section we’ll define a do_look() function that gets called when the
player types l or look.
Demo
A: Define do_look()#
[ ]Define ado_look()function.[ ]In it, use thedebug()function to print something like"Trying to look around.".
Code
122def do_look():
123 """Look at the current place"""
124
125 debug("Trying to look around.")
126
B: Modify main(), in the while loop#
[ ]Add anelifthat checks ifcommandis"l"or"look".[ ]if so, calldo_look()
Code
223 if command in ["q", "quit", "exit"]:
224 do_quit()
225
226 elif command in ("shop"):
227 do_shop()
228
229 elif command in ("g", "go"):
230 do_go(args)
231
232 elif command in ("x", "exam", "examine"):
233 do_examine(args)
234
235 elif command in ("l", "look"):
236 do_look()
237
238 else:
239 error("No such command.")
240 continue
241
242 print()
243
Part 5.2: Print place name and description#
In this section we’ll look up the place info and print the name and description.
Demo
A: Modify do_look(): look up and print the current place#
[ ]get the value fromPLAYERassociated with the"place"key and assign it toplace_name[ ]get the value fromPLACESassociated withplace_nameand assign it toplace[ ]Print the value associated with the"name"key of theplacedictionary using theheader()function.[ ]Print the value associated with the"description"key of theplacedictionary using thewrap()function.
Code
122def do_look():
123 """Look at the current place"""
124
125 debug("Trying to look around.")
126
127 # look up where the player is now
128 place_name = PLAYER["place"]
129 place = PLACES[place_name]
130
131 # print information about the current place
132 header(f"{place['name']}")
133 wrap(place["description"])
134
Part 5.3: Print the place items#
In this section we’ll print the list of items in the current place.
Demo
A: Modify do_look(), at the end#
In this section you will use each of the items in the current places "items"
list to get the item information from ITEMS then construct a list of each
items "name".
[ ]Use the.get()method on theplacedictionary to get the value associated with theitemskey with a default value of[], and assign it to the variableitems.[ ]Ifitemsis truthy:[ ]Make an empty list assigned to the variablenames[ ]Iterate over theitemslist using the variable namekeyfor each item. For each item:[ ]Get the value fromITEMSassociated with thekeykey and assign it to the variableitem[ ]Append the value associated with the"name"key from theitemsdictionary to thenameslist
Code
122def do_look():
123 """Look at the current place"""
124
125 debug("Trying to look around.")
126
127 # look up where the player is now
128 place_name = PLAYER["place"]
129 place = PLACES[place_name]
130
131 # print information about the current place
132 header(f"{place['name']}")
133 wrap(place["description"])
134
135 # get the items in the room
136 items = place.get("items", [])
137
138 if items:
139 # for each of the place items
140 # get the info from the ITEMS dictionary
141 # and make a list of item names
142 names = []
143 for key in items:
144 item = ITEMS.get(key)
145 names.append(item["name"])
B: Modify do_look(), in if items#
In this section we’re going to construct a plain English sentence listing the items in this place. If there is only one item it will look like:
y
If there are two items it will look like:
x and y
And if there are three or more items it will look like:
x, x and y
[ ]Remove the last item from thenameslist using the.pop()method and assign it to the variablelast.[ ]Join thenameslist using", "as a delimiter and assign it to the variabletext[ ]Iftextis truthy then append" and "to text.[ ]Appendlasttotext[ ]Print a blank line[ ]Use thewrite()function to print:You see text.
Code
138 if items:
139 # for each of the place items
140 # get the info from the ITEMS dictionary
141 # and make a list of item names
142 names = []
143 for key in items:
144 item = ITEMS.get(key)
145 names.append(item["name"])
146
147 # remove the last name from the list
148 last = names.pop()
149
150 # construct a sentence that looks like one of:
151 # x, x and y
152 # x and y
153 # y
154 text = ", ".join(names)
155 if text:
156 text += " and "
157 text += last
158
159 # print the list of items.
160 print()
161 wrap(f"You see {text}.\n")
162
Part 5.4: Print the nearby places#
In this section we’ll print the name of each of any places directly to the
"north", "south", "east" or "west" of the players current place.
Demo
A: Modify do_look(), at the end#
[ ]print a blank line[ ]Use a for loop to iterate over a list:"north","east","south", and"west"using the variable namedirection. For each one:[ ]Get the value associated with thedirectionkey from theplacedictionary and assign it to the variablename.[ ]Ifnameis falsy, then continue
[ ]Get the place dictionary fromPLACESassociated with thenamekey and assign it todestination.[ ]Use thewrite()function to print:"To the direction is name.". Getnamefrom thedestinationdictionary.
Code
122def do_look():
123 """Look at the current place"""
124
125 debug("Trying to look around.")
126
127 # look up where the player is now
128 place_name = PLAYER["place"]
129 place = PLACES[place_name]
130
131 # print information about the current place
132 header(f"{place['name']}")
133 wrap(place["description"])
134
135 # get the items in the room
136 items = place.get("items", [])
137
138 if items:
139 # for each of the place items
140 # get the info from the ITEMS dictionary
141 # and make a list of item names
142 names = []
143 for key in items:
144 item = ITEMS.get(key)
145 names.append(item["name"])
146
147 # remove the last name from the list
148 last = names.pop()
149
150 # construct a sentence that looks like one of:
151 # x, x and y
152 # x and y
153 # y
154 text = ", ".join(names)
155 if text:
156 text += " and "
157 text += last
158
159 # print the list of items.
160 print()
161 wrap(f"You see {text}.\n")
162
163 # add a blank line
164 print()
165
166 # print what is in each direction from here
167 for direction in ("north", "east", "south", "west"):
168 name = place.get(direction)
169 if not name:
170 continue
171
172 destination = PLACES.get(name)
173 write(f"To the {direction} is {destination['name']}.")
174