0
class Employee:

def set_name(self, name):
    self.name = name

def set_IDnumber(self, IDnumber):
    self.IDnumber = IDnumber

def set_department(self, department):
    self.department = department

def set_jobTitle(self, jobTitle):
    self.jobTitle = jobTitle

def get_name(self):
    # Returns name
    return self.name

def get_IDnumber(self):
    # Returns IDnumber
    return self.IDnumber

def get_department(self):
    # Returns department
    return self.department

def get_jobTitle(self):
    # Returns job title
    return self.jobTitle

def make_list():
    # Declaring list for storing information
    employee_list = []
    print("Enter data for 3 employees")
    # Loop to loop through 3 employees info
    for i in range(1, 4):
        print('Employee ' + str(i))
        # Takes input for each object
        name = input('Enter employee name: ')
        idNumber = int(input('Enter employee ID number: '))
        department = input('Enter employee department: ')
        jobTitle = input('Enter job title: ')
        employee = Employee()
        # Sets information for each object
        employee.set_name(name)
        employee.set_IDnumber(idNumber)
        employee.set_department(department)
        employee.set_jobTitle(jobTitle)
        employee_list.append(employee)
    #Returns list for display
    return employee_list

def displayEmployees(list):
    # Declaring strings to be further manipulated for spacing purposes
    shopString = "CHRISTY'S SHOP EMPLOYEE REPORT"
    employeeString = "EMPLOYEE NAME"
    identifierString = "IDENTIFIER"
    departmentString = "DEPARTMENT"
    titleString = "TITLE"
    # String alignment
    print(shopString.center(70))
    print(employeeString + identifierString.center(40) + departmentString +     titleString.rjust(10))
    print("------------------" + "----------".center(30) + "----------".rjust(15) + "-----".rjust(10))
    for item in list:
        name = item.get_name()
        idNumber = item.get_IDnumber()
        department = item.get_department()
        jobTitle = item.get_jobTitle()
        print(name + idNumber.center(30) + department.rjust(15) + jobTitle.rjust(10))


def main():
    employees = make_list()
    displayEmployees(employees)

main()

Here's my program. I'm trying to format the output from the list like this below:

enter image description here

If anyone could help me because I find that due to the differing length of each string in the list throws off the alignment so there must be a better way to align the information correctly. Thanks ahead of time.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • 1
    You are coming from a java background aren't you? – e4c5 Mar 11 '17 at 01:31
  • Yes I am, don't know how you noticed but thanks for noticing. :) – jean wilders Mar 11 '17 at 01:32
  • You want string methods. https://docs.python.org/3.6/tutorial/inputoutput.html#fancier-output-formatting. Particularly `ljust`, `center`, `rjust`. – Denziloe Mar 11 '17 at 01:32
  • @Denziloe he seems to be using them already – e4c5 Mar 11 '17 at 01:34
  • 1
    I guesse because we don't use getters and setters much in python – e4c5 Mar 11 '17 at 01:34
  • please add a few sample lines of input (as text) – e4c5 Mar 11 '17 at 01:35
  • @e4c5 Huh, I missed that. Well I dunno mate, it shouldn't be too hard to format a nice table using string methods. – Denziloe Mar 11 '17 at 01:36
  • [Tutorial](http://www.python-course.eu/python3_formatted_output.php) and [document](http://www.python-course.eu/python3_formatted_output.php) – Prune Mar 11 '17 at 01:37
  • Also: try to use properties (get/set) [more pythonic way](http://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work) . – MaLiN2223 Mar 11 '17 at 02:01
  • Indentation is very important in Python. Please make sure all your code is indented properly. To make it show up as code, rather that just hitting Space four times on the first line, select __all__ of your code and press CTRL+K. – Nic Mar 11 '17 at 06:26

2 Answers2

0

Using only the relevant portion of your code, and the Format Specification Mini-Language, here's an attempt:

# String alignment
print('{:^62}'.format(shopString))
print('{:<22}{:^20}{:<19}{:<15}'.format(employeeString, identifierString, departmentString, titleString))
print('{:<22}{:^20}{:<19}{:<15}'.format("------------------", "----------", "--------------", "-----------"))
for item in list:
    name = item.get_name()
    idNumber = item.get_IDnumber()
    department = item.get_department()
    jobTitle = item.get_jobTitle()
    print('{:<22}{:^20}{:<19}{:<15}'.format(name, idNumber, department, jobTitle))

Output:

                CHRISTY'S SHOP EMPLOYEE REPORT                
EMPLOYEE NAME              IDENTIFIER     DEPARTMENT         TITLE          
------------------         ----------     --------------     -----------    
Lily Thomas                   412         Administration     Manager        
John Doe                      132         Accounting         Accountant     
LaQuanda Shaka                321         Sales              Associate

Surely it's straightforward enough to modify and get it formatted as you wish.

0

CODE ON THE END

I changed a few things in your code.

Firstly, since you have no methods that are not accesors i used namedtuple instead of your Employee class. If you really need getters and setters (in class) please read this loudly and make it sink in:

If I must have them then I will hide them behind a property.

(paraphrased quote from here). By property I mean true pythonic properties. However try not to do this, see : this answer

Secondly, you have to check if user gave you good type (hence try-except).

Thirdly, I used something called Format Specification Mini-Language (a.k.a. formatspec) . It really helps if it comes to string formatting.

from collections import namedtuple

Employee = namedtuple('EmployeeRecord', 'name,idNumber,department,jobTitle')


def make_list():
    employees = []
    print("Enter data for 3 employees")
    i = 1
    while i < 4:
        print('Employee ' + str(i))

        name = input('Enter employee name: ')
        idNumber = input('Enter employee ID number: ')
        try:
            idNumber = int(idNumber)
        except ValueError:
            print("ID number is supposted to be integer")
            continue
        idNumber = int(idNumber)
        department = input('Enter employee department: ')
        jobTitle = input('Enter job title: ')
        employees.append(Employee(name,idNumber,department,jobTitle)) 
        i += 1
    return employees


def displayEmployees(list, titleFormat, recordFormat):
    shopString = "CHRISTY'S SHOP EMPLOYEE REPORT"
    print('{:^70}'.format(shopString))
    print(titleFormat.format("EMPLOYEE NAME", "IDENTIFIER", "DEPARTMENT", "TITLE"))
    print(titleFormat.format("------------------", "----------", "----------", "-----"))
    for item in list:
        name = item.name
        idNumber = item.idNumber
        department = item.department
        jobTitle = item.jobTitle
        print(recordFormat.format(name, idNumber, department, jobTitle))


def main():
    employees = make_list() # you can uncomment this line
    titleFormat = "{:<25}{:^15}{:<15}{:<10}"
    recordFormat = "{:<23}{:^19}{:<13}{:<10}"
    displayEmployees(employees, titleFormat, recordFormat)



def main2():
    e1 = Employee("name1", 1, "dep1", "title1")
    e2 = Employee("name2", 2, "dep2", "title2")
    e3 = Employee("name3", 3, "dep3", "title3")
    titleFormat = "{:<25}{:^15}{:<15}{:<10}"
    recordFormat = "{:<23}{:^19}{:<13}{:<10}"
    employees = [e1, e2, e3]
    displayEmployees(employees, titleFormat, recordFormat)

#main()
main2()

This is what I got from main2():

                    CHRISTY'S SHOP EMPLOYEE REPORT                    
EMPLOYEE NAME              IDENTIFIER   DEPARTMENT     TITLE     
------------------         ----------   ----------     -----     
name1                           1         dep1         title1    
name2                           2         dep2         title2    
name3                           3         dep3         title3   

If you want to know more about python string formatting here you can find a great tutorial.

Also I would like to recommend one of my favourite articles about java vs python python is not java

Community
  • 1
  • 1
MaLiN2223
  • 1,290
  • 3
  • 19
  • 40