Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid KeyError when working with dictionaries?

Right now I'm trying to code an assembler but I keep getting this error:

Traceback (most recent call last):
  File "/Users/Douglas/Documents/NeWS.py", line 44, in 
    if item in registerTable[item]:
KeyError: 'LD'

I currently have this code:

functionTable = {"ADD":"00",
         "SUB":"01",
         "LD" :"10"}

registerTable = {"R0":"00",
         "R1":"00",
         "R2":"00",
         "R3":"00"}

accumulatorTable = {"A"  :"00",
            "B"  :"10",
            "A+B":"11"}

conditionTable = {"JH":"1"}

valueTable = {"0":"0000",
          "1":"0001",
          "2":"0010",
          "3":"0011",
          "4":"0100",
          "5":"0101",
          "6":"0110",
          "7":"0111",
          "8":"1000",
          "9":"1001",
          "10":"1010",
          "11":"1011",
          "12":"1100",
          "13":"1101",
          "14":"1110",
          "15":"1111"}

source = "LD R3 15"

newS = source.split(" ")

for item in newS:

        if item in functionTable[item]:
            functionField = functionTable[item]
        else:
            functionField = "00"

        if item in registerTable[item]:
            registerField = registerTable[item]
        else:
            registerField = "00"

print(functionField + registerField)

Help is appreciated.

like image 275
Wigan Pier Avatar asked Sep 03 '17 14:09

Wigan Pier


People also ask

How do I get rid of KeyError in Python?

A Python KeyError is raised when you try to access an item in a dictionary that does not exist. You can fix this error by modifying your program to select an item from a dictionary that does exist. Or you can handle this error by checking if a key exists first.

How do you handle KeyError?

The Usual Solution: . If the KeyError is raised from a failed dictionary key lookup in your own code, you can use . get() to return either the value found at the specified key or a default value.

What is KeyError dictionary?

exception KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys.

How do I fix KeyError 0 Python with a dictionary?

The Python "KeyError: 0" exception is caused when we try to access a 0 key in a a dictionary that doesn't contain the key. To solve the error, set the key in the dictionary before trying to access it or conditionally set it if it doesn't exist.


3 Answers

You generally use .get with a default

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

So when you use get the loop would look like this:

for item in newS:     functionField = functionTable.get(item, "00")     registerField = registerTable.get(item, "00")     print(functionField + registerField) 

which prints:

1000 0000 0000 

If you want to do the explicit check if the key is in the dictionary you have to check if the key is in the dictionary (without indexing!).

For example:

if item in functionTable:   # checks if "item" is a *key* in the dict "functionTable"     functionField = functionTable[item]  # store the *value* for the *key* "item" else:     functionField = "00" 

But the get method makes the code shorter and faster, so I wouldn't actually use the latter approach. It was just to point out why your code failed.

like image 102
MSeifert Avatar answered Sep 28 '22 08:09

MSeifert


There is no key 'LD' in registerTable. Can put a try except block :

try:
   a=registerTable[item]
      ...
except KeyError:
   pass
like image 35
akp Avatar answered Sep 28 '22 07:09

akp


You are looking to see if the potential key item exists in in dictionary at item. You simply need to remove the lookup in the test.

if item in functionTable:
    ...

Though this could even be improved.

It looks like you try to look up the item, or default to '00'. Python dictionaries has the built in function .get(key, default) to try to get a value, or default to something else.

Try:

functionField = functionTable.get(item, '00')
registerField = registerTable.get(item, '00')
like image 30
Kyle P Avatar answered Sep 28 '22 07:09

Kyle P