0
inventory = {
    'gold' : 500, 
    'sack' : ['rock', 'copper wire'],
    'weapons rack' : ['pistol', 'MP-5', 'grenade'],
    'ammo pouch' : ['Pistol ammo', 'MP-5 ammo'],
}

print "You have " + inventory['gold'][0] + " coins!"

I get the error message:

  line 8, in <module>
    print "You have " + inventory['gold'] + " coins!"
TypeError: 'int' object has no attribute '__getitem__'

why wouldn't the console print out "You have 500 gold coins!"

1 Answer 1

2

Your gold entry is not a list; you are trying to index the 500 integer. Remove the [0]:

print "You have", inventory['gold'], "coins!"

Note that I used commas, not +, because you cannot just concatenate strings and integers like that. The alternative would be to convert the integer to a string first:

print "You have " + str(inventory['gold']) + " coins!"

You could also put the gold value into a list:

inventory = {
    'gold' : [500], 
    'sack' : ['rock', 'copper wire'],
    'weapons rack' : ['pistol', 'MP-5', 'grenade'],
    'ammo pouch' : ['Pistol ammo', 'MP-5 ammo'],
}

Note the [...] square brackets around the 500 value there. Then your [0] applies again:

print "You have", inventory['gold'][0], "coins!"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.