我正试图使用pop函数删除列表中的第四个元素。然而,我不断收到一条错误消息。请帮忙。
inventory = ["twin bed", "twin bed", "headboard", "queen bed", "king bed", "dresser",
"dresser", "table", "table", "nightstand", "nightstand", "king bed",
"king bed", "twin bed", "twin bed", "sheets", "sheets", "pillow",
"pillow"]
removed_item = inventory.pop[4]
print(removed_item)
这是不断返回的错误消息;
Traceback (most recent call last):
File "script.py", line 18, in <module>
removed_item = inventory.pop[4]
TypeError: 'builtin_function_or_method' object is not subscriptable
是的,你是对的,pop()
确实有一个索引,但你不像pop[4]
那样写,所以就像quamrana说的那样,你应该放inventory.pop(4)
而不是inventory.pop[4]