Home > Software design >  What's syntax if i need to update some employee?
What's syntax if i need to update some employee?

Time:02-14

Emp_name = []
Emp_id = []
Emp_dep = []
num = 5
for i in range(num):
    x = input("enter the name of employees")
    Emp_name.append(x)

    i = input("enter the id of employees")
    Emp_id.append(i)

    d = input("enter the department of employees")
    Emp_dep.append(d)
delete = str(input(" Enter the name of employee you need to delete"))
if delete in Emp_name:
    pos = Emp_name.index(delete)
    Emp_name.pop(pos)
    Emp_id.pop(pos)
    Emp_dep.pop(pos)

print(Emp_name)
print(Emp_id)
print(Emp_dep)

If I need to update some employee what's syntax tp fo that? I am working in employee application

CodePudding user response:

The simplest way to update existing data in the array is by getting the index of the employee to update (as you're doing for deletion) and then updating the values for name and department in the respective arrays. Like this, for example:

update = str(input(" Enter id of employee you want to update"))
if update in Emp_id:
    pos = Emp_id.index(update)
    Emp_name[pos] = str(input(" Enter new name"))
    Emp_dep[pos] = str(input(" Enter new department"))

CodePudding user response:

to update employess I have modified your code and added update logic:

Emp_name = []
Emp_id = []
Emp_dep = []
num = 3
for i in range(num):
    x = input("enter the name of employees")
    Emp_name.append(x)

    i = input("enter the id of employees")
    Emp_id.append(i)

    d = input("enter the department of employees")
    Emp_dep.append(d)

#UPDATING employee

id = str(input(" Enter the id of employee you need to update"))
if id in Emp_id:
    pos = Emp_id.index(id)
    Emp_name[pos] = input("enter new name : ")
    Emp_id[pos] = input("enter new id : ")
    Emp_dep[pos] = input("enter new department : ")

print(Emp_name)
print(Emp_id)
print(Emp_dep)

Use a class of Employee type and create objects of it . It will help you to manage employees much efficiently.

class Employee:
    def __init__(self, name="", id="", dept=""):
        self.name = name
        self.id = id
        self.dept = dept

    def __str__(self):
        return self.name   " "   self.id   " "   self.dept


employees = list()

num = 3

for i in range(num):
    name = input("enter the name of employees : ")
    id = input("enter the id of employees : ")
    dept = input("enter the department of employees : ")

    emp = Employee(name, id, dept)
    employees.append(emp)

for e in employees:
    print(e)

delete = input(" Enter the name of employee you need to delete : ")

for index, emp in enumerate(employees):
    if emp.name == delete:
        employees.pop(index)
        break

for e in employees:
    print(e)

Output:

python/src/test/employee.py
enter the name of employees : dev
enter the id of employees : 1
enter the department of employees : dept
enter the name of employees : dev2
enter the id of employees : dept
enter the department of employees : dept
enter the name of employees : dev
enter the id of employees : 3
enter the department of employees : dept
dev 1 dept
dev2 dept dept
dev 3 dept
Enter the name of employee you need to delete : dev
dev2 dept dept
dev 3 dept
  • Related