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 <module> 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"} 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: r
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.