Part 8: Drop things
Contents
Part 8: Drop things#
In this section we’ll add the drop
command.
Part 8.1: Add command#
In this section we’ll define a do_drop()
function that gets called when the
player types drop
.
Demo
A: Define do_drop()
#
[ ]
Define ado_drop()
function.[ ]
In it, use thedebug()
function to print something like"Trying to drop args."
.
Code
315def do_drop(args):
316 """Remove an item from inventory"""
317
318 debug(f"Trying to drop: {args}.")
B: Modify main()
, in the while loop#
[ ]
Add anelif
that checks ifcommand
is"drop"
.[ ]
if so, calldo_drop()
and passargs
.
Code
335 if command in ("q", "quit", "exit"):
336 do_quit()
337
338 elif command in ("shop"):
339 do_shop()
340
341 elif command in ("g", "go"):
342 do_go(args)
343
344 elif command in ("x", "exam", "examine"):
345 do_examine(args)
346
347 elif command in ("l", "look"):
348 do_look()
349
350 elif command in ("t", "take", "grab"):
351 do_take(args)
352
353 elif command in ("i", "inventory"):
354 do_inventory()
355
356 elif command == "drop":
357 do_drop(args)
358
359 else:
360 error("No such command.")
361 continue
Part 8.2: Validate#
In this section we’ll check to make sure that the player entered an item they have in inventory.
Demo
A: Modify do_drop()
#
[ ]
Check to see ifargs
is falsy, if so:[ ]
Use theerror()
function to print a message saying:"What do you want to drop?"
[ ]
return
[ ]
assign the first item of theargs
list to the variablename
and make it lowercase[ ]
Check ifname
is not inPLAYER["inventory"]
or ifPLAYER["inventory"][name]
is falsy. If so:[ ]
Use theerror()
function to print a message saying:"You don't have any name."
[ ]
return
Code
315def do_drop(args):
316 """Remove an item from inventory"""
317
318 debug(f"Trying to drop: {args}.")
319
320 # make sure the player typed an item
321 if not args:
322 error("What do you want to drop?")
323 return
324
325 # get the item name from arguments
326 # and make it lowercase
327 name = args[0].lower()
328
329 # make sure the item is in inventory
330 if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331 error(f"You don't have any {name!r}.")
332 return
333
Part 8.3: Drop it#
In this section we’ll check remove the item from the players inventory and add it to the place items.
Demo
A: Modify do_drop()
: remove from inventory#
[ ]
subtract1
fromPLAYER["inventory"][name]
[ ]
remove item from inventory if the quantity is0
by:if
PLAYER["inventory"][name]
is falsy:[ ]
call.pop()
onPLAYER["inventory"]
with the argumentname
Code
315def do_drop(args):
316 """Remove an item from inventory"""
317
318 debug(f"Trying to drop: {args}.")
319
320 # make sure the player typed an item
321 if not args:
322 error("What do you want to drop?")
323 return
324
325 # get the item name from arguments
326 # and make it lowercase
327 name = args[0].lower()
328
329 # make sure the item is in inventory
330 if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331 error(f"You don't have any {name!r}.")
332 return
333
334 # remove from inventory
335 PLAYER["inventory"][name] -= 1
336 if not PLAYER["inventory"][name]:
337 PLAYER["inventory"].pop(name)
338
B: Modify do_drop()
: add to 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
[ ]
call.setdefault()
onplace
with the argument"items"
and[]
[ ]
appendname
toplace["items"]
[ ]
print a message using thewrap()
function like:You set down the name.
Code
315def do_drop(args):
316 """Remove an item from inventory"""
317
318 debug(f"Trying to drop: {args}.")
319
320 # make sure the player typed an item
321 if not args:
322 error("What do you want to drop?")
323 return
324
325 # get the item name from arguments
326 # and make it lowercase
327 name = args[0].lower()
328
329 # make sure the item is in inventory
330 if name not in PLAYER["inventory"] or not PLAYER["inventory"][name]:
331 error(f"You don't have any {name!r}.")
332 return
333
334 # remove from inventory
335 PLAYER["inventory"][name] -= 1
336 if not PLAYER["inventory"][name]:
337 PLAYER["inventory"].pop(name)
338
339 # look up where the player is now
340 place_name = PLAYER["place"]
341 place = PLACES[place_name]
342
343 # add to place items
344 place.setdefault("items", [])
345 place["items"].append(name)
346
347 # print a message
348 wrap(f"You set down the {name}.")