Home > OS >  create a list from class user input
create a list from class user input

Time:11-13

  • Create an employee class with the following members: name, age, id, salary
  • setData() - should allow employee data to be set via user input
  • getData()- should output employee data to the console
  • create a list of 5 employees. You can create a list of objects in the following way, appending the objects to the lists.
  emp_object = []
  for i in range(5):
    emp_ object.append(ClassName())

I'm trying to do this exercise and this is what I got:

class employee:
    def __init__(self, n = None, a = None, i = None, s = None):
        self.name = n
        self.age = a
        self.id = i
        self.salary = s
    def setData(self):
        self.n = input("Enter name: ")
        self.a = int(input("Enter age: "))
        self.i = int(input("Enter id: "))
        self.s = int(input("Enter salary: "))
        self.getData()
    def getData(self):
        print("Name:", self.name, self.age, self.id, self.salary)

e1 = employee()
e1.setData()

e2 = employee()
e2.setData()

e3 = employee()
e3.setData()

e4 = employee()
e4.setData()

e5 = employee()
e5.setData()

emp_object = []
for i in range(5):
    emp_object.append(employee())
print(emp_object)

It prints the employee details as "None" and I need help to create a list

Expected Output:

Name             id      Age     Salary
AAA              20      1       2000
BBB              22      2       2500
CCC              20      3       1500
DDD              22      4       3500
EEE              22      5       4000

CodePudding user response:

First you want to separate some responsabilities for a better reading. We will divide the problem in two parts :

  1. Employee model
  2. Input/output problem

Employee

Create a class who contains only employee data (we can use dataclasses but, I assume you're a beginner, so I'll keep simple)

class Employee:
    def __init__(self, uid=None, name=None, age=None, salary=None):
        self.name = name
        self.age = age
        self.id = uid
        self.salary = salary

Output and Input

To display the employee's data in console, we can use __str__ function. It is used when you class need to be converted into a str (in print for isntance). We then add an other method in charge to set employee's data. Our Employee class become :

class Employee:
    def __init__(self, uid=None, name=None, age=None, salary=None):
        self.name = name
        self.age = age
        self.id = uid
        self.salary = salary

    def __str__(self):
        return f"Name: {self.name}, {self.age}, {self.id}, {self.salary}"

    def set_data(self):
        self.name = input("Enter name: ")
        self.age = int(input("Enter age: "))
        self.id = int(input("Enter id: "))
        self.salary = int(input("Enter salary: "))

Our class is complete. Now we will write the algorithm in charge to create 5 employees. So under the Employee class :

if __name__ == '__main__':
    # Empty list containing our employees
    employees = []
    # We loop 5 times.
    for i in range(5):
        # We create an employee
        employee = Employee()
        # We set the data
        employee.set_data()
        # We append our brand-new employee into the list
        employees.append(employee)

    # Now we display our data :
    for employee in employees:
        # We just need to print the object thanks to __str__ method
        print(employee)

Tell me if I answered correctly to your problem !

CodePudding user response:

Change the instance variable self.n ( in the setData method) to self.name to match the declaration your class init method ...and do the same for the self.a, self.i... variables .

CodePudding user response:

I beleive the problem is that you are not setting the parameters to the ones you want in the setData function.

You need to do this:

class employee:
    def __init__(self, n = None, a = None, i = None, s = None):
        self.name = n
        self.age = a
        self.id = i
        self.salary = s
    def setData(self):
        self.name = input("Enter name: ")
        self.age = int(input("Enter age: "))
        self.id = int(input("Enter id: "))
        self.salary = int(input("Enter salary: "))
        self.getData()
    def getData(self):
        print("Name:", self.name, self.age, self.id, self.salary)

The __init__ and setData are two separate functions.

  • Related