Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm very new to coding, and I'm still confused about how to approach this. I want to make an employee database that can, depending on user input 1: lookup an employee, 2: add an employee, 3: change an existing employee's info, and 4: list all employees.

I can get the first and last specifications to work (albeit messily), but I'm having trouble with 2 and 3. I'm also unsure about the right way to store my employee instances, or if I should store them in a dictionary instead. Any pointers on how to start steps 2 and 3 would be great!

I've tried looking up a few different solutions, as well as reading articles about classes and/or dictionaries, but I'm unfortunately lost.

Defining the class:

class Employee:

```def __init__(self, name, salary, department, title):

``````self.name = name

``````self.salary = salary

``````self.department = department

``````self.title = title

```def tell(self):

``````print('{}, {}, {}, {}'.format(self.name, self.salary, self.department, self.title), end="")

```def add_employee(self, addname, addsalary, adddepartment, addtitle):

``````self.name, self.salary, self.department, self.title.append(addname, 

``````addsalary, adddepartment, addtitle)

The above is nested under the class; the last line is an attempt to make an add_employee function that adds employee details from user_input to the pre-set instances. I don't think this code is correct, either.

The following is a function outside of the class, as well as the three pre-set employee instances; my guess is this is also bad code (oof):

def add_employee_input():

```Employee.add_employee(input("Enter name:"), input("Enter salary:"), input("Enter department:"),input("Enter title:"))

```print("The full roster is as follows:")

```emp1.tell()

```emp2.tell()

```emp3.tell()

```emp4.tell()

emp1 = Employee('Angela', '40000', 'Department for Penguin Research', 'Penguinologist')

emp2 = Employee('Mr. Rogers', '60000', 'Admin', 'Manager')

emp3 = Employee('Lucie', '50000', 'Department for Snail Research', 'Snailologist')

emp4 = Employee(addname, addsalary, adddepartment, addtitle)

I know the last line is wrong; I don't know if I can directly insert a variable into a class instance like this. And I doubt the function is correct, either.

If I try to run the code without the add_employee function blocked off, I get an error message about how the "addname," "addsalary," etc. variables are undefined. This makes sense to me since I'm trying to incorporate local class variables to an outside function. I don't know how to do it otherwise, but I'm sure there's a way.

1 Answer

0 votes
by (19.9k points)

Your function needs an employee to set different part of it

def add_employee_input(employee: Employee):

  employee.name = input("Name:")

  ...

You also need an empty constructor.

class Employee:

  def __init__(self):

    self.name = "DefaultName"

    ...

Related questions

0 votes
1 answer
asked Jul 11, 2020 in Python by ashely (50.2k points)
0 votes
4 answers
0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...