所以我正在开发一个程序,它曾经保存/加载很好,我有所有正确的导入等等,但是最近,当我尝试加载时,我一直收到此错误消息(和类似的保存期间一个:
Traceback (most recent call last):
File "C:\Users\Adam\AppData\Local\Programs\Python\Python36\lib\shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'flipvariables'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\BascGames\ManorI\MANOR.py", line 241, in <module>
flipvariables = sf['flipvariables']
File "C:\Users\Adam\AppData\Local\Programs\Python\Python36\lib\shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
File "C:\Users\Adam\AppData\Local\Programs\Python\Python36\lib\dbm\dumb.py", line 147, in __getitem__
pos, siz = self._index[key] # may raise KeyError
KeyError: b'flipvariables'
抛出错误的代码(它在两个地方,两个不同的时间,但它们具有完全相同的代码 - 这是重载代码,使用SHELVE):
flipvariables = {'papercomb': '', 'ropegiver': 'north', 'riddlenum': 1, 'theriddle': 'hi', 'riddleanswer': '?', 'passcode': '0', 'timesrealived': 0, 'rockkeydropped': 'no', 'maximumriddleguesses': 3, 'clutzmode': 'off', 'shortdesc': 'no', 'shortkeyss': {'qzz': 'die'}, 'shortkeys': ['qzz'], 'shortcommands': ['die'], 'saplasts': 5, 'riddlefailed': 'no', 'longdesc': 'no', 'waited': 0, 'intrivia': 'no', 'minimumtrivia': 2, 'goblinscore': 0, 'thicketscut': 'not', 'gascanfilled': 'no', 'lockout': 3, 'alarmactivated': 'on', 'holedug': 'no', 'wearinggloves': 'no', 'skeletonkicked': 'no', 'gettingtetanus': 'no', 'tetanusgotten': 'no', 'tetanusin': 3, 'turntoexplosion': 3, 'boatfixed': 'no', 'gargoylestatus': 'locked', 'safestatus': 'locked', 'disturbeddirtstatus': 'locked', 'scannerstatus': 'locked', 'toychestlookedat': 0, 'seedsplanted': 'no', 'flowersin': 4, 'flowersplanted': 'no'}
#abridged version of the flipvariables mentioned in the code
first == input(">").lowercase()
if(first == 'reload'):
slot = input("\nSavefile 1, 2 or 3? ").lower()
if(slot == '1') or (slot == 'one'):
cpon = os.path.isfile('C:\\BascGames\\ManorI\\SaveFiles\\1.dat')
elif(slot == '2') or (slot == 'two'):
cpon = os.path.isfile('C:\\BascGames\\ManorI\\SaveFiles\\2.dat')
elif(slot == '3') or (slot == 'three'):
cpon = os.path.isfile('C:\\BascGames\\ManorI\\SaveFiles\\3.dat')
else:
cpon = os.path.isfile('C:\\BascGames\\ManorI\\SaveFiles\\0.dat')
if(cpon == False):
print("\nThat savefile does not exist.")
else:
if(slot == '1') or (slot == 'one'):
sf = shelve.open('C:\\BascGames\\ManorI\\SaveFiles\\1')
elif(slot == '2') or (slot == 'two'):
sf = shelve.open('C:\\BascGames\\ManorI\\SaveFiles\\2')
elif(slot == '3') or (slot == 'three'):
sf = shelve.open('C:\\BascGames\\ManorI\\SaveFiles\\3')
else:
sf = shelve.open('C:\\BascGames\\ManorI\\SaveFiles\\0')
print("\nSavefile not avaliable. Opened savefile 0.\n")
flipvariables = sf['flipvariables']
inventory = sf['inventory']
goblinstatuses = sf['goblinstatuses']
roominventories = sf['roominventories']
iteminventories = sf['iteminventories']
health = sf['health']
answersgot = sf['answersgot']
avalweight = sf['avalweight']
currentroom = sf['currentroom']
yname = sf['yname']
statuses = sf['statuses']
gametoughness = sf['gametoughness']
sf.close()
print("\nReload sucessful!")
flipvariables['roomchanged'] = 'yes'
有没有人知道这可能是什么以及如何解决它?提前致谢。我理解基础知识,它对线条意味着什么,但错误的基本思想对我来说没有意义。谢谢!
答案 0 :(得分:0)
键错误是指字典的键不存在时。例如,
person = {name: 'joe', age: 41}
如果我使用last_name
调用密钥person['last_name']
,Python将抛出KeyError
,因为给定字典的密钥不存在。
在你的错误中:
KeyError: b'flipvariables'
表示给定字典不存在键flipvariables
。
根据回溯中的行flipvariables = sf['flipvariables']
,查看代码中的sf
字典,查看flipvariables
键是否存在,并在必要时添加。