It's Dangerous to Go Alone! Take This


#1



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"]
}
inventory["backpack"].sort()
inventory["backpack"].remove("dagger")
inventory["gold"].append(50)

# 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'
inventory['pouch'].sort()


#2

.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 +=


#3

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.


#4

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().


#5

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.


#8

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


#9

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