i am making a game and i am getting a strange error
heres the relevant code

items = {'sword':['sword', 3, 15],
             'axe':['axe', 5, 25],
             'bow and arrow':['bow and arrow', 4, 20]}
for item in items:
            print items[item[0]]
            print '\tprice:' + items[item[1]]
            print '\tdamage:' + items[item[2]]

Heres my error:
27, in <module>
menu()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 6
2, in menu
menu_input()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 7
0, in menu_input
load_game()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 1
21, in load_game
adventure_menu()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 1
45, in adventure_menu
adventure_menu_input()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 1
52, in adventure_menu_input
store()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 1
87, in store
store()
File "C:\Documents and Settings\tom\Desktop\Awesome text game\main.py", line 1
99, in store
if p.weapon == items[sitem[1]]:
KeyError: 'o'


any help would be appreciated

Recommended Answers

All 2 Replies

Your "items" dictionary doesn't have a key named "o" which apparently you are trying to locate. The line if p.weapon == items[sitem[1]] is basically equating to if p.weapon == items["o"] . That seems to not be in the chunk of "relevant code" that you posted, but even so, the code you posted throws an error on its own.

Regarding the code, you shouldn't cycle the dictionary "items" like you are currently doing. Cycle the list of the values in the dictionary, and unpack them to variable names like this:

items = {'sword':['sword', 3, 15],
    'axe':['axe', 5, 25],
    'bow and arrow':['bow and arrow', 4, 20]}
for item, price, damage in items.values():
    print item
    print "\tprice", price
    print "\tdamage", damage

Your items dictionary is a little repetitive though, by putting the item name as both the key and the first index in the value of it. This is much better:

items = {'sword':[3, 15],
    'axe':[5, 25],
    'bow and arrow':[4, 20]}
# cycle through the (key, value) list of the dict
for item, stats in items.items():
    print item
    print "\tprice", stats[0]
    print "\tdamage", stats[1]

Hope that helped!

commented: very nice example +7

You can do this two different ways.

items = {'sword':['sword', 3, 15],
             'axe':['axe', 5, 25],
             'bow and arrow':['bow and arrow', 4, 20]}

##---- the easiest to understand
for key in items:
   print "key =", key
   weapon_list = items[key]
   print "\tweapon_list =", weapon_list
   print "\tprice =", weapon_list[1]
   print "\tdamage =", weapon_list[2]
   
##--- the way you tried to do it
for key in items:
   print items[key][0]
   print '\tprice:', items[key][1]
   print '\tdamage:', items[key][2]
commented: good explanation +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.