2016-11-18 51 views
-4
#Homework 10 Question 6 Zachariah Huckstep 
#Employee Management System 

#Employee Class 
class Employee(): 
    #Init method 
    def __init__(self,empName,empID,empDept,empTitle): 
     #Set self name, id number, department and job title 
     self.name=empName 
     self.idnum=empID 
     self.dept=empDept 
     self.title=empTitle 

    #set_name method 
    def set_name(self,empName): 
     #Set the name 
     self.__empName=empName 

    #set_idnum method 
    def set_idnum(self,empID): 
     #Set the id number 
     self.__empID=empID 

    #set_dept method 
    def set_dept(self,empDept): 
     #Set the department 
     self.__empDept=empDept 

    #set_title method 
    def set_title(self,empTitle): 
     #Set the title 
     self.__empTitle=empTitle 

    #get_name method 
    def get_name(self): 
     #Get the name 
     return self.__empName 

    #get_idnum method 
    def get_idnum(self): 
     #Get the id number 
     return self.__empID 

    #get_dept method 
    def get_dept(self): 
     #Get the department 
     return self.__empDept 

    #get_title method 
    def get_title(self): 
     #Get the title 
     return self.__empTitle 

    #__str__ method 
    def __str__(self): 
     #Return object state as a string 
     return "Name: "+self.__empName+\ 
       "\nID Number: "+self.__empID+\ 
       "\nDepartment: "+self.__empDept+\ 
       "\nJob Title: "+self.__empTitle 

#Start of program 
import pickle 

#Menu choices 
lookup=1 
add=2 
change=3 
delete=4 
quit=5 

#File name 
fileName='staff.db' 

#main function 
def main(): 
    #Load the existing employee database as a dictionary 
    #and assign it to staff 
    staff=load_staff() 

    #Set users option 
    opt=0 

    #Process menu options until quit 
    while opt != quit: 
     #Get the user's menu option 
     opt=get_menu_option() 

     #Decide what to do 
     if opt == lookup: 
      lookup(staff) 
     elif opt == add: 
      add(staff) 
     elif opt == change: 
      change(staff) 
     elif opt == delete: 
      delete(staff) 

    #Save staff dictionary to staff.db 
    save_staff(staff) 

#Load staff function 
def load_staff(): 
    #Try opening staff.db 
    try: 
     #Open the staff.db file 
     file=open(fileName,'rb') 

     #Unpickle the dictionary 
     staff_dct=pickle.load(file) 

     #Close the staff.db file 
     file.close() 
    except IOError: 
     #Could not find file, so make an 
     #empty dictionary 
     staff_dct={} 

    #Return the staff database as a dictionary 
    return staff_dct 

#Print menu options and get opt 
def get_menu_option(): 
    #Print menu 
    print() 
    print('Menu:') 
    print('1. Look up an employee') 
    print('2. Add a new employee') 
    print('3. Change an existing employee') 
    print('4. Delete an employee entry') 
    print('5. Quit the Employee Management System') 
    print() 

    #Get opt 
    opt=int(input('=>')) 

    #Validate opt 
    while opt < lookup or choice > quit: 
     #Get valid opt 
     opt=int(input('Enter valid choice: ')) 

    #Return opt 
    return opt 

#Lookup function 
def lookup(staff): 
    #Ask for the employee ID 
    empID=input('Enter empoyee ID: ') 

    #Search employee database for employee ID 
    print(staff.get(empID,'Invalid ID Number.') 

#Add function 
def add(staff): 
    #Ask for employee info 
    print('Please enter the following new employee information:') 
    empName=input('Name: ') 
    empID=input('ID Number: ') 
    empDept=input('Department: ') 
    empTitle=input('Job Title: ') 

    #Create a current employee object named currEmp 
    currEmp=Employee(empName,empID,empDept,empTitle) 

    #If the ID number is invalid, add it as a key 
    #with the currEmp object as the value 
    if empID not in staff: 
     #Add currEmp into staff 
     staff[empID]=currEmp 
     print('New employee added.') 
    else: 
     #Print error 
     print('That employee ID already exists.') 

#Change function 
def change(staff): 
    #Ask for employee ID 
    empID=input('Enter Employee ID: ') 

    if empID in staff: 
     #Ask for new name 
     empName=input('Enter new name: ') 
     #Ask for new department 
     empDept=input('Enter new department: ') 
     #Ask for new job title 
     empTitle=input('Enter new job title: ') 

     #Create an employee object named currEmp 
     currEmp=Employee(empName,empID,empDept,empTitle) 

     #Update the employee information 
     staff[empID]=currEmp 
    else: 
     #Print error 
     print('Error: Invalid employee ID.') 

#Delete function 
def delete(staff): 
    #Ask for employee ID 
    empID=input('Enter employee ID: ') 

    #If the employee ID is found, delete it from staff 
    if empID in staff: 
     #Delete it 
     del staff[empID] 
     print('Employee deleted from database.') 
    else: 
     #Print error 
     print('Error: Invalid employee ID.') 

#Save staff function 
def save_staff(staff): 
    #Pickle the staff object and save it to staff.db 
    #Open file for writing 
    file=open(fileName,'wb') 

    #Pickle staff database and save it 
    pickle.dump(staff,file) 

    #Close the file 
    file.close() 

#Call the main function 
main() 

这个程序是一个员工管理系统,我昨晚写完了,现在我无法运行它。它具有154线运行故障(添加功能)Python类和主要功能没有运行?

Python是告诉我这一点: 高清加(职员): ^ 语法错误:无效的语法

请帮助。

+1

你应该花时间阅读[mcve]。具体是__minimal__部分。 –

+3

您在前面的print调用中省略了括号:print(staff.get(empID,'无效的ID号')'。 –

回答

3

您缺少一个圆括号。

#Search employee database for employee ID 
print(staff.get(empID,'Invalid ID Number.') 

应该是:

#Search employee database for employee ID 
print(staff.get(empID,'Invalid ID Number.')) 
0

的错误是在你的查找功能,你还没有把第二个右括号中的print()。下面是校正代码

#Lookup function 
    def lookup(staff): 
     #Ask for the employee ID 
     empID=input('Enter empoyee ID: ') 

     #Search employee database for employee ID 
     print(staff.get(empID,'Invalid ID Number.')) 

我只加查找()函数。

希望它有帮助:)