title: How do I avoid KeyError when working with dictionaries?
body: <p>Right now I'm trying to code an assembler but I keep getting this error:</p>
  
  <pre>
  Traceback (most recent call last):
    File "/Users/Douglas/Documents/NeWS.py", line 44, in 
      if item in registerTable[item]:
  KeyError: 'LD'
  </pre>
  
  <p>I currently have this code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>Help is appreciated.</p>