1

I'm trying to solve this problem on my own. The problem asks to write a class for employee information, then ask user to input that information, and finally print the entered information.

I want to use two for loops, one for getting information and one for printing the information. Unfortunately, the printing for loop does not working.

class Employee:
    def __init__(self, name, id_num, department, job):
        self.__name = name
        self.__id_num = id_num
        self.__department = department
        self.__job = job

    # setters
    def set_name(self,name):
        self.__name = name
    def set_id(self,id_num):
        self.__id_num = id_num
    def set_department(self,department):
        self.__department = department
    def set_job(self,job):
        self.__job = job

    # getters
    def get_name(self):
        return self.__name
    def get_id(self):
        return self.__id_num
    def get_department(self):
        return self.__department
    def get_job(self):
        return self.__job

def main():

    employee_list = []

    for i in range(2):
        name = input('What is the name of the employee? ')
        id_num = float(input('What is the ID number of the employee? '))
        department = input('What is the department where the employee works? ')
        job = input('What is the job title of the empoyee? ')

        personnel = Employee(name,id_num,department,job)

        employee_list.append(personnel)
    return employee_list


    for item in employee_list:
        print(item.get_name())
        print(item.get_id())
        print(item.get_department())
        print(item.get_job())
        print()


main()
2
  • 1
    As an aside, your style is not very pythonic here. It looks like you are writing "java in python". Don't write getters and setters, they are not necessary and only add unnecessary clutter. Don't use double underscores, use a single underscore to signal that this attribute is not part of the public interface. Double-underscore name-mangling is to prevent name collisions in subclasses. Commented Jul 7, 2019 at 1:07
  • @juanpa.arrivillaga Thanks for the style comments. I'm self-learning from the book "starting out with python". On page 449 it says to hide attributes in Python we use double underscores. I didn't know single underscore works too. Commented Jul 7, 2019 at 1:19

1 Answer 1

2

You need to remove the following line in your main() function:

return employee_list

It is causing your main to stop running without ever reaching the printing for loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.