2017-12-27 6 views
-1

Hallo Ich arbeite an einem Python-Text-Abenteuer und ich habe eine Speicherfunktion, die alle Hauptvariablen Inventar, Standort und Gold gespeichert. Dann fügte ich 2 weitere Variablen hinzu und es würde nicht funktionieren. Vielen Dank im Voraus.Python-Beize IndexError: Tupel-Index außerhalb des Bereichs

Hier ist mein Arbeitscode.

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

Dies ist der Code, der nicht

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

Die Fehlermeldung Ich erhalte Indexerror ist funktioniert: Tupelindex

außerhalb des Bereichs
+1

Was ist der genaue Fehler mit Trace? 'loadValues' enthält wahrscheinlich nicht so viele Elemente, wie Sie denken. Haben Sie überprüft, was es enthält? – Carcigenicate

+1

Die 2 Schnipsel scheinen auf den ersten Blick korrekt zu sein. Bist du sicher, dass du 'do_save' nicht aus dem ersten und' do_load' aus dem zweiten Snippet mixst? Versuchen Sie, 'loadValues' zu drucken, bevor Sie etwas damit machen. Auch "arg" scheint nicht verwendet zu werden. – CristiFati

Antwort

0

ich mit einer Lösung kam, aber es möglicherweise nicht der effizienteste Weg hier ist der Code

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) 
Verwandte Themen