Question
Python program, probelm with output. When i call my lookup(emp) function its supposed to print an employees name, id and department if the ID number
Python program, probelm with output. When i call my lookup(emp) function its supposed to print an employees name, id and department if the ID number entered by the user exists in the dictionary. Otherwise it'll print out a message saying that id isn't in the dictionary. the program imports a class from another program ,lab1a, that uses this class Employee to set and get values for name, id and department which are stored in a dictionary. The output of my lookup(emp) function should look like this after entering an id that exists in the dictionary:
Enter an employee ID number: "employee" Name: "employees name" ID number: "employees id number" Department: "whatever department goes with this employee"
instead my output for lookup(emp) looks like this:
Please enter an employee id number to search: 8949
the print function used should reference __str__ function in my Employee class to print like its supposed to but its. not
provided below is the code which has my class Employee - (lab1a)
class Employee: def __init__(self,name,ID,department): self.__name = name self.__ID = ID self.__department = department def set_name(self,employee_name): self.__name = employee_name def set_ID(self,employee_ID): self.__ID = employee_ID def set_department(self, employee_department): self.__department = employee_department def get_name(self): return self.__name def get_ID(self): return self.__ID def get_department(self): return self.__department def __str__(self): result = 'Name: ' + self.get_name() + \ ' ID number: ' + self.get_ID() + \ ' Department: ' + self.get_department() return result
finally here is my code for the program that imports the employee class and has issues with the lookup(emp) function:
import lab1a import pickle
def main(): emp = open_dict() user_pick = 0 while user_pick != 5: print("Employee Management System Menu") print("Please enter a choice from the list below:") print("Lookup = 1, Add = 2, Change = 3, Delete = 4 or Quit = 5") user_pick = int(input("Type in desired choice: ")) if user_pick == 1: look_up(emp) elif user_pick == 2: add(emp) elif user_pick == 3: change(emp) elif user_pick == 4: delete(emp) else: print("Program terminated!") store_dict(emp) def open_dict(): try: file = open('emp.dat','rb') emp_dict = pickle.load(file) file.close() except IOError: emp_dict = {} return emp_dict
def store_dict(emp): update_file = open('emp_dict.dat', 'wb') pickle.dump(emp, update_file) update_file.close()
def look_up(emp): lookup_id = input("Please enter an employee id number to search: ") print(emp.get(lookup_id,"ID number not in system!"))
def add(emp): add_id = input("Please enter a new employee id: ") add_name = input("Please enter a new employee name: ") add_department = input("Please enter a new employee deparment: ") add_emp = lab1a.Employee(add_name, add_id, add_department) if add_id in emp: print("Error, new employee not added. Id number already exists!") else: emp[add_id] = add_emp print("Employee added!")
def change(emp): change_id = input("Please enter an employee id to change: ") if change_id in emp: change_name = input("Please change name of employee: ") change_department = input("Please change deparment of employee: ") change_emp = lab1a.Employee(change_name, change_id, change_department) emp[change_id] = change_emp print("Employee Changed!") else: print("That id number provided doesn't exist!")
def delete(emp): del_id = input("Please enter an employee id to delete: ") if del_id in emp: del emp[del_id] print("Employee deleted!") else: print("That id number provided doesn't exist!")
if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started