【发布时间】:2017-10-09 13:19:43
【问题描述】:
我正在使用字典来编译有关股票等的数据,但是当我引用用户输入的 gtin 代码时,我收到错误“列表索引必须是整数或切片,而不是 str - Python”
这是导致此问题的唯一代码部分:
tempStockLvl = [num]['STOCK_LEVEL'] # This is the line the error references
tempStockLvl = int(tempStockLvl)
tempStockLvl = tempStockLvl - quantity
stock[num]['STOCK_LEVEL'] = tempStockLvl
错误:
File "E:\Computer Science\CODE FINAL v2.py", line 206, in subtractQuant
tempStockLvl = [num]['STOCK_LEVEL']
TypeError: list indices must be integers or slices, not str
提前感谢任何回答的人:D
【问题讨论】:
-
num的值是多少? -
这是一个八位数字,即 12345670(GTIN 代码)
-
[num]['STOCK_LEVEL']- 您是否试图索引某种嵌套数据结构?something[num]['STOCK_LEVEL']? -
另外,您发布的代码显然与错误消息的来源代码不同。
-
同样的问题:对象丢失(可能只是
stock)。
标签: python string dictionary integer indices