2017-12-27 5 views
-1

こんにちは私は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() 

これは私が得ているエラーメッセージははIndexErrorある

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() 

が動作していないコードです:範囲

+1

トレースとの正確なエラーは何ですか?おそらく 'loadValues'にはあなたが思うほど多くの要素が含まれていないでしょう。あなたはそれが何を含んでいるか確認しましたか? – Carcigenicate

+1

2つのスニペットが一見して正しいようです。第1のスニペットの 'do_save'と第2のスニペットの' do_load'を混ぜていないのですか?何かをする前に 'loadValues'を印刷してみてください。また、 'arg'は使われていないようです。 – CristiFati

答えて

0

のうちタプルインデックスは、私は解決策を考え出したが、それここでコードの中で最も効率的な方法ではないかもしれません。

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) 
関連する問題