-2

I'm using a dictionary to compile data about stocks and such but when I come to reference the gtin code input by the user, i get the error 'List indices must be integers or slices, not str - Python'

This is the only section of code that causes this:

tempStockLvl = [num]['STOCK_LEVEL']  # This is the line the error references
tempStockLvl = int(tempStockLvl)
tempStockLvl = tempStockLvl - quantity
stock[num]['STOCK_LEVEL'] = tempStockLvl

Error:

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

Thanks in advance to anyone who answers :D

AntsOfTheSky
  • 195
  • 2
  • 3
  • 17

1 Answers1

1

You're creating a List with num as the only element, so it's clear it can't be accessed by anything other than index [0].

>>> num = 123
>>> l = [num]
>>> l
[123]
>>> l[0]
123
>>> [123]['anything']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>>

Did you mean to write tempStockLvl = stock[num]['STOCK_LEVEL']?

handle
  • 5,859
  • 3
  • 54
  • 82