Part 6: Take things
Contents
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()
#
[ ]
Define ado_take()
function.[ ]
In it, use thedebug()
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#
[ ]
Add anelif
that checks ifcommand
is"t"
,"take"
or"grab"
.[ ]
if so, calldo_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
#
[ ]
For any item you wish for the player to be able totake
, 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#
[ ]
Check to see ifargs
is falsy, if so:[ ]
Use theerror()
function to print a message saying:"Which way do you want to go?"
[ ]
return
[ ]
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
[ ]
assign the first item of theargs
list to the variablename
and make it lowercase[ ]
Get the list of items in theplace
dictionary using.get()
with a default value of[]
. Check to see ifname
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#
[ ]
Using.get()
, get the value fromITEMS
associated with thename
key and assign it to the variableitem
.[ ]
Ifitem
is falsy,[ ]
Print an error message like:"Woops! The information about name!r seems to be missing."
[ ]
return
[ ]
Using.get()
, get the value fromitem
associated with the"can_take"
key. Check to see if it is falsy. If so:[ ]
Usewrap()
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
#
[ ]
Add"inventory": {}
to thePLAYER
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.
[ ]
Call.setdefault()
onPLAYER["inventory"]
with the argumentsname
and0
.This will set the current inventory for
name
to0
if it is not already in our inventory.[ ]
Add1
toPLAYER["inventory"][name]
[ ]
Removename
from theplace["items"]
list using the.remove()
method[ ]
Use thewrap()
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()
#
[ ]
Find the if statement where you check ifname
is not in theplace
items list. Modify it so that it shows the error ifname
is not inplace
items andname
is not inPLAYER["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