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()#

  1. [ ] Define a do_look() function.

  2. [ ] In it, use the debug() 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#

  1. [ ] Add an elif that checks if command is "l" or "look".

    • [ ] if so, call do_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#

  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. [ ] Print the value associated with the "name" key of the place dictionary using the header() function.

  4. [ ] Print the value associated with the "description" key of the place dictionary using the wrap() 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".

  1. [ ] Use the .get() method on the place dictionary to get the value associated with the items key with a default value of [], and assign it to the variable items.

  2. [ ] If items is truthy:

    1. [ ] Make an empty list assigned to the variable names

    2. [ ] Iterate over the items list using the variable name key for each item. For each item:

      • [ ] Get the value from ITEMS associated with the key key and assign it to the variable item

      • [ ] Append the value associated with the "name" key from the items dictionary to the names list

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
  1. [ ] Remove the last item from the names list using the .pop() method and assign it to the variable last.

  2. [ ] Join the names list using ", " as a delimiter and assign it to the variable text

  3. [ ] If text is truthy then append " and " to text.

  4. [ ] Append last to text

  5. [ ] Print a blank line

  6. [ ] Use the write() 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#

  1. [ ] print a blank line

  2. [ ] Use a for loop to iterate over a list: "north", "east", "south", and "west" using the variable name direction. For each one:

    • [ ] Get the value associated with the direction key from the place dictionary and assign it to the variable name.

    • [ ] If name is falsy, then continue

  3. [ ] Get the place dictionary from PLACES associated with the name key and assign it to destination.

  4. [ ] Use the write() function to print: "To the direction is name.". Get name from the destination dictionary.

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