Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting employee from list
#1
Can anyone tell me please why my delete_employee() function doesn't actually delete an employee from the list, I'm banging my head against a wall all day trying to get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
Reply
#2
"in" does not work the way you are trying to use it.
1
if firstName in employee and secondName in employee:
You should have something like this:
1
if firstName == employee.firstName and secondName == employee.secondName:
You should have a function to find an employee by attribute and return a list of matches. This could be used to delete employees or to select an employee by name so you can edit the employee's attributes.

I do not like how you use getters and setters in your code. Why have a function named get_hours_worked that only returns hours_worked attribute's value. You can get that directly using "employee.hours_worked".

The Employee __init__ method should not have arguments for job title, amount per hour, hours worked or total wage. These values will change over the tenure of the employee. The __init__ method should only change things that don't change, or at least don't change very often. You should also use date of birth instead of age. I wouldn't want to update my employee database every day just to keep the age field current.
Reply
#3
Thanks for replying but I get an error "AttributeError list object has no attribute firstName"
Reply
#4
Sorry
1
2
1
if firstName == i.firstName and secondName == i.secondName:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  deleting select items from a list Skaperen 13 6,651 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  plot multiple employee sales data in a single graph pitanshu 0 2,359 Oct-24-2019, 01:56 PM
Last Post: pitanshu
  Deleting the first item in linked list dan789 7 4,987 Mar-05-2019, 06:34 PM
Last Post: ichabod801
  Deleting from a list of objects buddih09 2 3,188 Nov-07-2018, 04:08 PM
Last Post: ichabod801
  How are these Employee objects stored? Vysero 2 4,005 Jul-13-2018, 12:05 PM
Last Post: buran
  List Comparisons and Deleting Valuebles KaleBosRatjes 4 4,526 May-13-2018, 02:14 PM
Last Post: volcano63
  deleting certain rows from multidimensional list aster 4 15,559 Nov-05-2017, 10:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020