Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have the following error when running printDicts() in my code:
Traceback (most recent call last):
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 238, in <module>
main()
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 232, in main
choisesDict[choise]()
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 206, in printDicts
d = add3dicts(d1,d2,d3)
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 198, in add3dicts
addD3(list(d3.keys()))
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 194, in addD3
return addD2(lis[1:])
File "Desktop\PythonApplication1\PythonApplication1\exercise3.py", line 184, in addD2
returnDic[lis[0]] = insert2(d2[lis[0]],d3[lis[0]])
KeyError: 8
The code is:
def add3dicts(d1,d2,d3):
insert3 = lambda x,y,z:tuple(set({x,y,z}))
insert2 = lambda x,y: tuple(set({x,y}))
returnDic = dict()
def addD1(lis):
if lis == []:
return
if(lis[0] in d2 and lis[0] in d3):
returnDic[lis[0]] = insert3(d1[lis[0]],d2[lis[0]],d3[lis[0]])
d2.pop(lis[0])
d3.pop(lis[0])
elif(lis[0] in d2):
returnDic[lis[0]] = insert2(d1[lis[0]],d2[lis[0]])
d2.pop(lis[0])
elif(lis[0] in d3):
returnDic[lis[0]] = insert2(d1[lis[0]],d3,[lis[0]])
d3.pop(lis[0])
else:
returnDic[lis[0]] = d1[lis[0]]
return addD1(lis[1:])
def addD2(lis):
if lis == []:
return
if(lis[0] in d3):
returnDic[lis[0]] = insert2(d2[lis[0]],d3[lis[0]])
d3.pop(lis[0])
else:
returnDic[lis[0]] = d2[lis[0]]
return addD2(lis[1:])
def addD3(lis):
if lis == []:
return
returnDic[lis[0]] = d3[lis[0]]
return addD2(lis[1:])
addD1(list(d1.keys()))
addD2(list(d2.keys()))
addD3(list(d3.keys()))
return returnDic
def printDicts():
d1 = eval(input("Please enter the first dictionary:"))
d2 = eval(input("Please enter the second dictionary:"))
d3 = eval(input("Please enter the third dictionary:"))
print("The merged dictionary is:")
d = add3dicts(d1,d2,d3)
print(d)
return
I tried everything I knew, searched the Internet and everything, and could not figure out what the error was and how to solve it.
Is the "key" in the error related to the dictionary? If so, then what's the error? The dictionary seems to be well built. If it's not related to the dictionary, then what's the error?
(Sorry if there are errors in English, this is not my native language ..)
–
–
Obviously, lis[0]
contains the value 8
at runtime. Then you try fetch the element with the key 8
in d2
and d3
. Either of these dictionaries doesn't contain an element with the given key. That's why the error is raised.
Your code only checks whether lis[0]
is in d3
, but not d2
. So d2
must be the culprit. Try changing your code to:
def addD2(lis):
if lis == []:
return
if(lis[0] in d3 and lis[0] in d2):
returnDic[lis[0]] = insert2(d2[lis[0]],d3[lis[0]])
d3.pop(lis[0])
elif lis[0] in d2:
returnDic[lis[0]] = d2[lis[0]]
Note: Since I'm not sure what the intended logic of the whole algorithm is, it might not give you the result you expect. But it should avoid the exception.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.