-1

I'm using a dictionary in Python and trying to reference one of the data categories however I am getting a keyerror when running the code.

"Restock_Item" will be an eight digit number such as "12345670" which is in the dictionary so by using

stock[restock_item]['STOCK_LEVEL']

it would be referencing the dictionary like this:

stock[12345670]['STOCK_LEVEL']

But when doing this I get the error:

KeyError: 12345670

Here is the code:

restock_item = input("\nPlease enter the gtin code of the product you wish to restock. ")
    if restock_item.isdigit() == True:

        restock_level = input("How many items would you like to restock? ")
        while restock_item.isdigit() == True:

            restock_item = int(restock_item)
            newStockLevel = int(stock[restock_item]['STOCK_LEVEL']) # This is the line that gets the key error
            newStockLevel = newStockLevel + restock_level
            stock[restock_item]['STOCK_LEVEL'] = newStockLevel
Jérôme
  • 13,328
  • 7
  • 56
  • 106
AntsOfTheSky
  • 195
  • 2
  • 3
  • 17
  • 3
    `KeyError` means that no item in your dictionary has that key. Are you sure that what you're trying to retrieve exists? – stelioslogothetis May 10 '17 at 20:23
  • 2
    `print` the dictionary. You'll find that you're trying to access a non-existent entry. Not much more to do – salezica May 10 '17 at 20:24
  • Are you creating stock[restock_item], or should it be there already? Or are you creating it unless it is already there? – Jérôme May 10 '17 at 20:25
  • Possible duplicate of [I'm getting Key error in python](http://stackoverflow.com/questions/10116518/im-getting-key-error-in-python) – Mad Physicist May 10 '17 at 20:26
  • @MadPhysicist im sorry it's just i dont understand it in other contexts – AntsOfTheSky May 10 '17 at 20:29
  • @Sty i'm sure it exists because the dictionary retrieves data from a file and the file has that key in it – AntsOfTheSky May 10 '17 at 20:30
  • @Jérôme it should be there already, im just accessing a piece of data from the dictionary and assigning it to a variable – AntsOfTheSky May 10 '17 at 20:32
  • 1
    When you retrieved the dictionary from the file, did you also apply `int()` to this key? `12345670` and `"12345670"` are not equivalent keys. – jasonharper May 10 '17 at 20:35
  • @jasonharper Yeah I just thought maybe that was the reason and i resolved the problem, thanks for the help :) i converted the variable to an int before i used it – AntsOfTheSky May 10 '17 at 20:37
  • 2
    You might want to 1/ Write that as an answer 2/ Accept your own answer so that this case is closed. – Jérôme May 10 '17 at 20:43

1 Answers1

0

Resolved in comments:

When you retrieved the dictionary from the file, did you also apply int() to this key?
12345670 and "12345670" are not equivalent keys. – jasonharper

Yeah I just thought maybe that was the reason and i resolved the problem, thanks for the help :) i converted the variable to an int before i used it – Heather Lara

stovfl
  • 14,998
  • 7
  • 24
  • 51