[nfbcs] Can anyone help me understand what I'm doing wrong here?
Lanie Molinar
laniemolinar91 at gmail.com
Wed Jul 25 01:33:16 UTC 2018
Hi, everyone. I'm learning Python 3 from a book called "Automate the
Boring Stuff with Python", and I just learned about dictionaries. I'm
now trying the practice projects at the end of the lesson, and I thought
I understood what I was doing until I got an error when I ran my code.
The project description is below. There are two in this lesson, but
they're short and one builds on the other, so I'm pasting both here.
Fantasy Game Inventory
You are creating a fantasy video game. The data structure to model the
player’s inventory will be a dictionary where the keys are string values
describing the item in the inventory and the value is an integer value
detailing how many of that item the player has. For example, the
dictionary value {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1,
'arrow': 12} means the player has 1 rope, 6 torches, 42 gold coins, and
so on.
Write a function named displayInventory() that would take any possible
“inventory” and display it like the following:
Inventory:
12 arrow
42 gold coin
1 rope
6 torch
1 dagger
Total number of items: 62
Hint: You can use a for loop to loop through all the keys in a dictionary.
# inventory.py
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def displayInventory(inventory):
print("Inventory:")
item_total = 0
for k, v in inventory.items():
# FILL IN THE CODE HERE
print("Total number of items: " + str(item_total))
displayInventory(stuff)
List to Dictionary Function for Fantasy Game Inventory
Imagine that a vanquished dragon’s loot is represented as a list of
strings like this:
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
Write a function named addToInventory(inventory, addedItems), where the
inventory parameter is a dictionary representing the player’s inventory
(like in the previous project) and the addedItems parameter is a list
like dragonLoot. The addToInventory() function should return a
dictionary that represents the updated inventory. Note that the
addedItems list can contain multiples of the same item. Your code could
look something like this:
def addToInventory(inventory, addedItems):
# your code goes here
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
The previous program (with your displayInventory() function from the
previous project) would output the following:
Inventory:
45 gold coin
1 rope
1 ruby
1 dagger
Total number of items: 48
My code is below:
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def displayInventory(inventory):
print("Inventory:")
item_total = 0
for k, v in inventory.items():
print(v, k)
item_total += v
print("Total number of items: " + str(item_total))
displayInventory(stuff)
def addToInventory(inventory, addedItems):
for i in addedItems:
inventory.setdefault(i, 1)
if i in inventory:
inventory[i] += 1
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
The exception I get says:
Message=AttributeError("'NoneType' object has no attribute 'items'")
Source=c:\users\lanie\source\repos\pythonpractice\fantasygameinventory\fantasygameinventory.py
StackTrace:
File
"c:\users\lanie\source\repos\pythonpractice\fantasygameinventory\fantasygameinventory.py",
line 6, in displayInventory
for k, v in inventory.items():
File
"c:\users\lanie\source\repos\pythonpractice\fantasygameinventory\fantasygameinventory.py",
line 22, in <module>
displayInventory(inv)
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd\_pydev_imps\_pydev_execfile.py",
line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd\pydevd.py",
line 1035, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd\pydevd.py",
line 1628, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\_main.py",
line 101, in _run
raise
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\_main.py",
line 47, in run_file
run(argv, addr, **kwargs)
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\packages\ptvsd\debugger.py",
line 36, in debug
run(address, filename, *args, **kwargs)
File "c:\program files (x86)\microsoft visual
studio\preview\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py",
line 111, in <module>
vspd.debug(filename, port_num, debug_id, debug_options, run_as)
It seems to think my inventory dictionary has the none type, and I don't
understand why. Can anyone see what I'm doing wrong? Thanks.
More information about the NFBCS
mailing list