class myClass:
def __init__(self):
self.myClassDict = {}
def ADD_DictPair(self, Value, Key):
#Per ShadowRanger's Solution (Solved)
#self.myClassDict[Value] = Key #Wrong Code
self.myClassDict[Key] = Value #Correct Code
def get_myDict(self):
return self.myClassDict
InstanceOfmyClass = myClass()
InstanceOfmyClass.ADD_DictPair("Value1","Key1")
InstanceOfmyClass.ADD_DictPair("Value2","Key3")
InstanceOfmyClass.ADD_DictPair("Value3","Key3")
print(InstanceOfmyClass.get_myDict()["Key1"])
Desired Output: "Value1"
Error: print(InstanceOfmyClass.get_myDict()["Key1"])
KeyError: 'Key1'
Windows中的Python3 // Sublime Text 3
我的目标是通过它的类方法与字典进行交互,以添加,调用和编辑值。
答案 0 :(得分:0)
def ADD_DictPair(self, Value, Key):
#Per ShadowRanger's Solution (Solved)
#self.myClassDict[Value] = Key #Wrong Code
self.myClassDict[Key] = Value #Correct Code