Python EOFError:输出错误消息

时间:2017-04-19 02:42:31

标签: python function dictionary

我一直试图让这本词典在Python中运行,但我却失败了。我在SO上发现了一些类似的解决方案,但是我无法让它们工作。我很确定它与我调用函数的方式有关。我已经尝试将每个部分放入一个单独的功能,但失败了。我仍然收到以下错误消息:

  

employee_dictionary = pickle.load(file)

     

EOFError:退出输入

这是我的代码:

import pickle
import os.path

def main():
    employee_dictionary = {}

    if os.path.exists("employee.dat"):
        file = open("employee.dat","rb")
        employee_dictionary = pickle.load(file)
        file.close()

    else:
        emp1 = Employee("Susan Myers", 47899, "Accounting", "Vice President")
        emp2 = Employee("Mark Jones", 39119, "IT", "Programmer")
        emp3 = Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")

        employee_dictionary = {emp1.get_ID_number(): emp1.get_name()+ '' + emp1.get_dept()+ '' + emp1.get_job_title(),\
                               emp2.get_ID_number(): emp2.get_name()+ '' + emp2.get_dept()+ '' + emp2.get_job_title(),\
                               emp3.get_ID_number(): emp3.get_name()+ '' + emp3.get_dept()+ '' + emp3.get_job_title()}

        return employee_dictionary

employee_dictionary = main()

choice = 'y'
while choice.upper()== 'Y':
    print("Make a selection from the following actions:")
    print("Lookup an employee in the dictionary: 1")
    print("Add a new employee to the dictionary: 2")
    print("Change an existing employee's name, department, and job title in the dictionary: 3")
    print("Delete an employee from the dictionary: 4")
    print("Quit the program: 5")
    selection = input("Make your selection: ")

    if int(selection) == 1:
        id_number = input("What is the employee's ID number?")

        if int(id_number) in employee_dictionary.keys():
            print(employee_dictionary[int(id_number)])

        else:
            print("The employee does not exist.")

    else:
        if int(selection) == 2:
            id_number = input("What is the employee's ID number?")

            if int(id_number) in employee_dictionary.keys():
                print('That employee already exists.')

            else:
                name = input("Enter the name of the employee:")
                dept = input("Enter the department of the employee:")
                title = input("Enter the job title of the employee:")
                emp4 = Employee(name,int(id_number), dept, title)
                employee_dictionary[emp4.get_ID_number()]= emp1.get_name()+ '' + emp4.get_dept()+ '' + emp4.get_job_title()

                print("The employee was added.")

        else:
            if int(selection) == 3:
                id_number = input("What is the employee's ID number?")

                if int(id_number) in employee_dictionary.keys():
                    name = input("Enter the name of the employee:")
                    dept = input("Enter the department of the employee:")
                    title = input("Enter the job title of the employee:")
                    emp4 = Employee(name,int(id_number), dept, title)
                    employee_dictionary[emp4.get_ID_number()]= emp1.get_name()+ '' + emp4.get_dept()+ '' + emp4.get_job_title()

                    print("The employee record has been updated.")

                else:
                    print("Record not found.")

            else:
                if int(selection) == 4:
                    id_number = input("What is the employee's ID number?")
                    print("Deleted: ", employee_dictionary.pop(int(id_number),"Record not found."))

                else:
                    if int(selection)!=5:
                        print("")

choice = input("Do you want to make another selection (y or n)?")
file = open('employee.dat','wb')
pickle.dump(employee_dictionary,file)
file.close()

这是原始的Employee类:

class Employee:
    def __init__(self, name, ID_number, dept, job_title):
        self.__name = name
        self.__ID_number = ID_number
        self.__dept = dept
        self.__job_title = job_title

    #set methods
    def set_name(self,name):
        self.__name = name
    def set_ID_number(self,ID_number):
        self.__ID_number = ID_number
    def set_dept(self,dept):
        self.__dept = dept
    def set_job_title(self,job_title):
        self.__job_title = job_title


    #get methods    
    def get_name(self):
        return self.__name
    def get_ID_number(self):
        return self.__ID_number
    def get_dept(self):
        return self.__dept
    def get_job_title(self):
        return self.__job_title

def main():

    emp1 = Employee("Susan Myers", 47899, "Accounting", "Vice President")
    emp2 = Employee("Mark Jones", 39119, "IT", "Programmer")
    emp3 = Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")

    #A bunch of print statements go here

main()

1 个答案:

答案 0 :(得分:0)

面向未来的用户。我想出了一个解决方案。这是我得到的:

import os.path
import pickle

class Employee:
    def __init__(self, name, ID_number, dept, job_title):
        self.__name = name
        self.__ID_number = ID_number
        self.__dept = dept
        self.__job_title = job_title

    #set methods
    def set_name(self,name):
        self.__name = name
    def set_ID_number(self,ID_number):
        self.__ID_number = ID_number
    def set_dept(self,dept):
        self.__dept = dept
    def set_job_title(self,job_title):
        self.__job_title = job_title


    #get methods    
    def get_name(self):
        return self.__name
    def get_ID_number(self):
        return self.__ID_number
    def get_dept(self):
        return self.__dept
    def get_job_title(self):
        return self.__job_title

def main():
    employee_dictionary = {}

    if os.path.exists("employee.dat"):
        file = open("employee.dat","rb")
        employee_dictionary = pickle.load(file)
        file.close()

    else:
        emp1 = Employee("Susan Myers", 47899, " Accounting", " Vice President")
        emp2 = Employee("Mark Jones", 39119, " IT", " Programmer")
        emp3 = Employee("Joy Rogers", 81774, " Manufacturing", " Engineer")

        employee_dictionary = {emp1.get_ID_number(): emp1.get_name()+ ',' + emp1.get_dept()+ ',' + emp1.get_job_title(),\
                               emp2.get_ID_number(): emp2.get_name()+ ',' + emp2.get_dept()+ ',' + emp2.get_job_title(),\
                               emp3.get_ID_number(): emp3.get_name()+ ',' + emp3.get_dept()+ ',' + emp3.get_job_title()}

        return employee_dictionary

employee_dictionary = main()

choice = 'y'
while choice.upper()== 'Y':
    print("Make a selection from the following actions:")
    print("Lookup an employee in the dictionary: 1")
    print("Add a new employee to the dictionary: 2")
    print("Change an existing employee's name, department, and job title in the dictionary: 3")
    print("Delete an employee from the dictionary: 4")
    print("Quit the program: 5\n")
    selection = input("Make your selection: ")
    print("\n")

    if int(selection) == 1:
        id_number = input("What is the employee's ID number?")

        if int(id_number) in employee_dictionary.keys():
            print("\nThe employee is:",employee_dictionary[int(id_number)],"\n")

        else:
            print("\nThe employee does not exist.\n")

    else:
        if int(selection) == 2:
            id_number = input("What is the employee's ID number?")

            if int(id_number) in employee_dictionary.keys():
                print('That employee already exists.\n')

            else:
                name = input("Enter the name of the employee:")
                dept = input("Enter the department of the employee:")
                title = input("Enter the job title of the employee:")
                emp4 = Employee(name,int(id_number), dept, title)
                employee_dictionary[emp4.get_ID_number()]= emp4.get_name()+ ', ' + emp4.get_dept()+ ', ' + emp4.get_job_title()

                print("\nThe employee was added.\n")

        else:
            if int(selection) == 3:
                id_number = input("What is the employee's ID number?")

                if int(id_number) in employee_dictionary.keys():
                    name = input("Enter the name of the employee:")
                    dept = input("Enter the department of the employee:")
                    title = input("Enter the job title of the employee:")
                    emp4 = Employee(name,int(id_number), dept, title)
                    employee_dictionary[emp4.get_ID_number()]= emp4.get_name()+ ', ' + emp4.get_dept()+ ', ' + emp4.get_job_title()

                    print("\nThe employee record has been updated.\n")

                else:
                    print("\nRecord not found.\n")

            else:
                if int(selection) == 4:
                    id_number = input("What is the employee's ID number?")
                    print("\nDeleted: ", employee_dictionary.pop(int(id_number),"Record not found."))
                    print("\n")

                else:
                    if int(selection)!=5:
                        print("")
                    break

choice = input("Do you want to make another selection (y or n)?")
file = open('employee.dat','wb')
pickle.dump(employee_dictionary,file)
file.close()