It's Dangerous to Go Alone! Take This


It's Dangerous to Go Alone! Take This

Traceback (most recent call last):
File "python", line 9, in
AttributeError: 'int' object has no attribute 'append'

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'],
    'pocket' : ["seashell", "strange", "berry", "lint"]

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'


.append() is to be used with lists, but in this case we are directly modifying the int that is stored inside of the dictionary. To append to an int use +=


Strings do not have an .append() method.

In the above, we are updating the value associated with the 'gold' key. Its type has no bearing.


You are correct in saying that .append() does not apply to strings, I was mistaken and was thinking of something else.

However, the type must certainly does have a bearing. The error message specifically addresses that because the value is an int we cannot use .append(). In addition, if we were were to modify the value of pouch we definitely would use .append().


The error message only says int because that is the detected type of the object being appended. The real error is attemping to append to a key value that is not a list.


A post was split to a new topic: What is the error?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.