Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Deleting employee from list

  • 04-01-2021 11:52pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭


    Can anyone tell me why my delete_employee() function doesn't actually delete an employee from the list, I just can't get it to work.

    class Employee(object):
     
        #Constructor
        def __init__(self,theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage):
            self.set_first_name(theFirstName)
            self.set_last_name(theLastName)
            self.age = theAge
            self.employeeNumber = theEmployeeNumber 
            self.jobTitle = theJobTitle
            self.amountPerHour = theAmountPerHour
            self.hoursWorked = theHoursWorked
            self.total_wage = self.calculate_wage()
     
        #Accessor Methods
        def get_first_name(self):
            return self.firstName
     
        def get_second_name(self):
            return self.secondName
     
        def get_age(self):
            return self.age
     
        def get_employee_number(self):
            return self.employeeNumber
     
        def get_job_title(self):
            return self.jobTitle
     
        def get_amount_per_hour(self):
            return self.amountPerHour
     
        def get_hours_worked(self):
            return self.hoursWorked
         
         
        #Mutator Methods
        def set_first_name(self,theFirstName):
            if not theFirstName:
                raise Exception("FIRST NAME FIELD CANNOT BE EMPTY!")
            self.firstName = theFirstName
     
        def set_last_name(self,theLastName):
            if not theLastName:
                raise Exception("LAST NAME FIELD CANNOT BE EMPTY!")
            self.lastName = theLastName
     
        def set_age(self,theAge):
            self.age = theAge
     
        def set_employee_number(self,theEmployeeNumber):
            self.employeeNumber = theEmployeeNumber
     
        def set_job_title(self,theJobTitle):
            self.jobTitle = theJobTitle
     
        def set_amount_per_hour(self,theAmountPerHour):
            self.amountPerHour = theAmountPerHour
     
        def set_hours_worked(self,theHoursWorked):
            self.hoursWorked = theHoursWorked
     
        # Calculate weekly wage
        def calculate_wage(self):
            return round(self.get_amount_per_hour() * self.get_hours_worked())
     
        #ToString Method
        def __str__(self):
            return '\nName: {} \nSecond Name:{} \nAge:{} \nEmployee Number:{} \nJob Title:{} \nAmount per Hour:{} \nHours ' \
                   'Worked:{} \nTotalWage:{} '.format(self.firstName, self.lastName, self.age, self.employeeNumber,
                                                      self.jobTitle, self.amountPerHour,
                                                      self.hoursWorked, self.calculate_wage())
     
    class Operator(Employee):
        def __init__(self,theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage):
            super().__init__(theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage)
     
     
        def __str__(self):
            return super(Operator,self).__str__()
     
    def enter_employee_details():
        firstName = input("Enter Employee Firstname: ")
        lastName = input("Enter Employee Lastname:   ")
        age = int(input("Enter Employee Age:        "))
        employeeNumber = input("Enter EmployeeNumber: ")
        jobTitle = input("Please enter Employee JobTitle: ")
        amountPerHour = float(input("Enter Employee Rate Per Hour: "))
        hoursWorked = int(input("Enter Employee HoursWorked for the week: "))
        return Operator(firstName,lastName,age,employeeNumber,jobTitle,amountPerHour,hoursWorked,total_wage = 0)
     
    def look_up_employee(employees):
        found = False
        firstName = input("Enter Employee's name: ")
        for employee in employees:
            if firstName in employee.get_first_name():
                print(employee)
                found = True
        if not found:
            print("NO SUCH EMPLOYEE FOUND! ")
     
    def show_all_employees(employees):
        print("Showing all Employees: ")
        for employee in employees:
            print(employee)
     
    def delete_employee(employee):
        print("\n**********Delete Employee***********")
        firstName = input("Please enter firstName:   ")
        secondName = input("Please enter secondName: ")
        for i in employee: 
            if firstName in employee and secondName in employee:
                employee.remove(i)
                break
     
    def menu_choices():
        print("\n*************Employee DataBase***************")
        print("1) New Employee.         2) Lookup Employee.")
        print("3) Show all Employee's.  4) Delete Employee.")
        print("5) Exit.")
     
     
    def exit_message():
        print("Exiting............")
     
    def main():
        option = 0
        employees = []
        running = True
        while running: 
            menu_choices()
            option = input(">")
            if option == "1":
                employees.append(enter_employee_details())
            elif option == "2":
                look_up_employee(employees)
            elif option == "3":
                show_all_employees(employees)
            elif option == "4":
                delete_employee(employees)
            elif option == "5":
                running = False
                exit_message()
            else:
                print("UNRECONIZED INPUT!")
     
      main()
    
    

    Any help appreciated


Comments

  • Administrators, Computer Games Moderators, Sports Moderators Posts: 32,531 Admin ✭✭✭✭✭Mickeroo


    Should the if statement inside the for loop not be checking if firstname and lastname are in i rather than employee?

    I'm a bit of a novice though so I could be way off. :o


  • Moderators, Music Moderators Posts: 23,363 Mod ✭✭✭✭feylya


    if firstName in employee and secondName in employee:
    

    That checks the entire array, not the individual element that you're parsing over. For clarity, it would make sense to change your delete_employee function to accept employees rather than employee


  • Registered Users, Registered Users 2 Posts: 1,807 ✭✭✭q2ice


    Can anyone tell me why my delete_employee() function doesn't actually delete an employee from the list, I just can't get it to work.

    class Employee(object):
     
        #Constructor
        def __init__(self,theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage):
            self.set_first_name(theFirstName)
            self.set_last_name(theLastName)
            self.age = theAge
            self.employeeNumber = theEmployeeNumber 
            self.jobTitle = theJobTitle
            self.amountPerHour = theAmountPerHour
            self.hoursWorked = theHoursWorked
            self.total_wage = self.calculate_wage()
     
        #Accessor Methods
        def get_first_name(self):
            return self.firstName
     
        def get_second_name(self):
            return self.secondName
     
        def get_age(self):
            return self.age
     
        def get_employee_number(self):
            return self.employeeNumber
     
        def get_job_title(self):
            return self.jobTitle
     
        def get_amount_per_hour(self):
            return self.amountPerHour
     
        def get_hours_worked(self):
            return self.hoursWorked
         
         
        #Mutator Methods
        def set_first_name(self,theFirstName):
            if not theFirstName:
                raise Exception("FIRST NAME FIELD CANNOT BE EMPTY!")
            self.firstName = theFirstName
     
        def set_last_name(self,theLastName):
            if not theLastName:
                raise Exception("LAST NAME FIELD CANNOT BE EMPTY!")
            self.lastName = theLastName
     
        def set_age(self,theAge):
            self.age = theAge
     
        def set_employee_number(self,theEmployeeNumber):
            self.employeeNumber = theEmployeeNumber
     
        def set_job_title(self,theJobTitle):
            self.jobTitle = theJobTitle
     
        def set_amount_per_hour(self,theAmountPerHour):
            self.amountPerHour = theAmountPerHour
     
        def set_hours_worked(self,theHoursWorked):
            self.hoursWorked = theHoursWorked
     
        # Calculate weekly wage
        def calculate_wage(self):
            return round(self.get_amount_per_hour() * self.get_hours_worked())
     
        #ToString Method
        def __str__(self):
            return '\nName: {} \nSecond Name:{} \nAge:{} \nEmployee Number:{} \nJob Title:{} \nAmount per Hour:{} \nHours ' \
                   'Worked:{} \nTotalWage:{} '.format(self.firstName, self.lastName, self.age, self.employeeNumber,
                                                      self.jobTitle, self.amountPerHour,
                                                      self.hoursWorked, self.calculate_wage())
     
    class Operator(Employee):
        def __init__(self,theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage):
            super().__init__(theFirstName,theLastName,theAge,theEmployeeNumber,theJobTitle,theAmountPerHour,theHoursWorked,total_wage)
     
     
        def __str__(self):
            return super(Operator,self).__str__()
     
    def enter_employee_details():
        firstName = input("Enter Employee Firstname: ")
        lastName = input("Enter Employee Lastname:   ")
        age = int(input("Enter Employee Age:        "))
        employeeNumber = input("Enter EmployeeNumber: ")
        jobTitle = input("Please enter Employee JobTitle: ")
        amountPerHour = float(input("Enter Employee Rate Per Hour: "))
        hoursWorked = int(input("Enter Employee HoursWorked for the week: "))
        return Operator(firstName,lastName,age,employeeNumber,jobTitle,amountPerHour,hoursWorked,total_wage = 0)
     
    def look_up_employee(employees):
        found = False
        firstName = input("Enter Employee's name: ")
        for employee in employees:
            if firstName in employee.get_first_name():
                print(employee)
                found = True
        if not found:
            print("NO SUCH EMPLOYEE FOUND! ")
     
    def show_all_employees(employees):
        print("Showing all Employees: ")
        for employee in employees:
            print(employee)
     
    def delete_employee(employee):
        print("\n**********Delete Employee***********")
        firstName = input("Please enter firstName:   ")
        secondName = input("Please enter secondName: ")
        for i in employee: 
            if firstName in employee and secondName in employee:
                employee.remove(i)
                break
     
    def menu_choices():
        print("\n*************Employee DataBase***************")
        print("1) New Employee.         2) Lookup Employee.")
        print("3) Show all Employee's.  4) Delete Employee.")
        print("5) Exit.")
     
     
    def exit_message():
        print("Exiting............")
     
    def main():
        option = 0
        employees = []
        running = True
        while running: 
            menu_choices()
            option = input(">")
            if option == "1":
                employees.append(enter_employee_details())
            elif option == "2":
                look_up_employee(employees)
            elif option == "3":
                show_all_employees(employees)
            elif option == "4":
                delete_employee(employees)
            elif option == "5":
                running = False
                exit_message()
            else:
                print("UNRECONIZED INPUT!")
     
      main()
    
    
    Any help appreciated


    I have no experience in Python (assuming thats the language you are coding in).
    Shouldn't you be checking if the firstName and secondName are in i and not in employee? employee looks like the object type while i is the current object you are interrogating (you are looping through all i's that are employee objects, thus the firstname and secondname lookup should be on the particular i instance)


    As I said, I do not have any python but all programming languages language logic looks similar


  • Registered Users, Registered Users 2 Posts: 63 ✭✭SilverSideUp


    @Mickeroo and @feylya are right. You should be checking the object i for firstName and lastName. Try this beginning on line 111:

    for i in employee: 
            if i.firstName == firstName and i.lastName == secondName:
                employee.remove(i)
                break
    

    i here is of type Operator.


  • Registered Users, Registered Users 2 Posts: 1,807 ✭✭✭q2ice


    @Mickeroo and @feylya are right. You should be checking the object i for firstName and lastName. Try this beginning on line 111:

    for i in employee: 
            if i.firstName == firstName and i.lastName == secondName:
                employee.remove(i)
                break
    
    i here is of type Operator.


    How does your code quote have i.firstName and i.secondName? Did you change it manually? Their code had firstName and secondName, which are local variables.
    Edit: i.lastName when their variable is secondName.

    Seriously, I think its just as simple a thing as checking if the firstName and secondName are present in i instead of the object type which is employee.

    Am really curious to know if that resolves their problem, but chances are that the op got their solution from somewhere else and will never update this thread.
    Last place I worked, the grads used to post problems in every forum they could see, but never replied with the solution as they got it off-line

    Edit #2: for i in something, means that you are iterating through a list of objects of type something and for each iteration the current object has a name of i, thus inside the loop, if you want to check any particular piece of data you want to check it against the current object and not the object type/list - which in this case is i

    Edit #3: not used of reverse checking as you did. I would have compared local variable against object variable but you did object variable against local vairable. its worth trying out the persons code above


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 63 ✭✭SilverSideUp


    q2ice wrote: »
    How does your code quote have i.firstName and i.secondName? Did you change it manually? Their code had firstName and secondName, which are local variables.
    Edit: i.lastName when their variable is secondName.

    Seriously, I think its just as simple a thing as checking if the firstName and secondName are present in i instead of the object type which is employee.

    Am really curious to know if that resolves their problem, but chances are that the op got their solution from somewhere else and will never update this thread.
    Last place I worked, the grads used to post problems in every forum they could see, but never replied with the solution as they got it off-line


    Yes, I edited it, ran it and tested it. Sure, what else would I be doing at 2 am :D


  • Registered Users, Registered Users 2 Posts: 1,807 ✭✭✭q2ice


    Yes, I edited it, ran it and tested it. Sure, what else would I be doing at 2 am :D




    :D:D:D:D:D


    At this time, and after a good few beers, I cannot be sure if thats sarcasm or not.


    Either way, thanks very much, best laugh I had in ages :D

    Edit: and you posted before I saw the logic in your reply (seen my third edit). Either way, your post is gold :D You are definitely a programmer (am too old to call them developers)


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    Thanks for the help lads yer solution worked, I understand why it works as well which is good .

    feylya wrote: »
    if firstName in employee and secondName in employee:
    

    That checks the entire array, not the individual element that you're parsing over. For clarity, it would make sense to change your delete_employee function to accept employees rather than employee

    Ya that should have been employees and not employee


Advertisement