酸洗物体的错误

时间:2018-03-19 21:34:17

标签: python pickle

我们正在用泡菜做练习,而且这段代码没有按照预期运行,请帮助我,这是代码(有些词是西班牙语,因为我来自美国):

public void findAvailable() {
    /* You will use this auxiliar variable to tell or remember if you have found an available spot*/
    boolean found = false;
    /* This variable is your row indexer, an accumulator that you will use to remember which row to access and how many of them have you accessed at a given time*/
    int row = 0;
    /*this translates as follows:
    WHILE I HAVEN'T FOUND (the free spot) AND I HAVEN'T VISITED EACH ROW OF
    SEATS*/
    while (!found && row < seats.length) {
        /*The same thing, but now it's a column indexer*/
        int seat = 0;
        /*this translates as follows:
        WHILE I HAVEN'T FOUND (the free spot) AND I HAVEN'T VISITED EACH COLUMN
        OR/OF SEATS*/
        while (!found && seat < seats[row].length) {
          // Checking whether the seat at "row, seat" is available
          if (seats[row][seat]) {
            // If it is, remember it so we can stop looking any further
            found = true;
            // Notify that you've found an empty seat at "row, column"
            System.out.println("Row = " + row + " Seat = " + seat);
          }
          // Keep looking for seats in the same row
          seat++;
        }
        // If no luck on this row, let's move to another one
        row++;
    }

    // if we've reached this point...
    if(!found) System.out.println("There are no seats available this time...");
}

它抛出了:

    import pickle

class persona:

    def __init__(self, nombre, genero, edad):
        self.nombre = nombre
        self.genero = genero
        self.edad = edad
        print("se ha creado una persona nueva con el nombre de: ", self.nombre)

    def __str__(self):
        return "{} {} {}".format(self.nombre, self.genero, self.edad)

class listaPersonas:

    personas = []

    def __init__(self):
        listaDePersonas = open("ficheroExterno", "ab+")
        listaDePersonas.seek(0)

        try:
            self.personas = pickle.load(listaDePersonas)
            print("Se cargaron {} personas del fichero externo".format(len(self.personas)))
        except:
            print("El fichero está vacío")

        finally:
            listaDePersonas.close()
            del(listaDePersonas)

    def agregarPersonas(self, p):
        self.personas.append(p)
        self.guardarPersonasEnFicheroExterno()

    def mostrarPersonas(self):
        for p in self.personas:
            print(p)

    def guardarPersonasEnFicheroExterno(self):
        listaDePersonas = open("ficheroExterno", "wb")
        pickle.dump(self.personas, listaDePersonas)
        listaDePersonas.close()
        del(listaDePersonas)

    def mostrarInfoFicheroExterno(self):
        print("La información sle fichero externo es la siguiente: ")
        for p in self.personas:
            print(p)


miLista = listaPersonas()
persona = persona("Sandra", "Femenino", 29)
miLista.agregarPersonas(persona)
miLista.mostrarInfoFicheroExterno()

我喜欢1 1/2小时,我看到这段代码,我想猜测是什么问题,但代码与我老师的代码相同。请帮我。我正在使用Sublime文本进行编码。

1 个答案:

答案 0 :(得分:6)

在这一行中,您已使用类的实例替换了您的类persona

persona = persona("Sandra", "Femenino", 29)

pickle正在尝试查找persona的类定义但不能,因为它不再具有名称。

不要试图在两件事上使用相同的名称;只有最后的作业才算数。 Standard style是将clalCase名称用于clasess,因此您可以为您的班级Persona命名。