python pickle IndexError:元组索引超出范围

嗨我正在进行一个python文本冒险,并且我有一个保存所有主要变量库存,位置和黄金的保存功能。然后我添加了2个变量,它不起作用。 在此先感谢。python pickle IndexError:元组索引超出范围

这是我的工作代码。

def do_save(self, arg): 

saveGame = open('savegame.txt', 'wb')

saveValues = (inventory, gold, location)

pickle.dump(saveValues, saveGame)

saveGame.close()

def do_load(self, arg):

global inventory

global gold

global location

global equiped

global health

loadGame = open('savegame.txt', 'rb')

loadValues = pickle.load(loadGame)

inventory = loadValues[0]

gold = loadValues[1]

location = loadValues[2]

loadGame.close()

这是不工作

def do_save(self, arg): 

saveGame = open('savegame.txt', 'wb')

saveValues = (inventory, gold, location, equiped, health)

pickle.dump(saveValues, saveGame)

saveGame.close()

def do_load(self, arg):

global inventory

global gold

global location

global equiped

global health

loadGame = open('savegame.txt', 'rb')

loadValues = pickle.load(loadGame)

inventory = loadValues[0]

gold = loadValues[1]

location = loadValues[2]

equiped = loadValues[3]

health = loadValues[4]

loadGame.close()

代码我得到的错误信息是IndexError:元组索引超出范围

回答:

我想出了一个解决方案,但它可能不是最有效的方法是代码

def do_save(self, arg): 

saveGame = open('savegame.txt', 'wb')

saveValues = (inventory, gold, location, equiped, health)

saveValues1 = (equiped, health)

pickle.dump(saveValues, saveGame)

pickle.dump(saveValues1, saveGame)

saveGame.close()

def do_load(self, arg):

global inventory

global gold

global location

global equiped

global health

loadGame = open('savegame.txt', 'rb')

loadValues = pickle.load(loadGame)

inventory = loadValues[0]

gold = loadValues[1]

location = loadValues[2]

equiped = loadValues[3]

health = loadValues[4]

loadGame.close()

displayLocation(location)

以上是 python pickle IndexError:元组索引超出范围 的全部内容, 来源链接: utcz.com/qa/265236.html

回到顶部