-2

In this pastebin link I have written python program to do ask for name,ID and job title and then return data.

But, whenever I run this program it is giving me an error saying "

line 50, in for employee in employeeList: NameError: name 'employeeList' is not defined

"

I pasted code on pastebin because stackoverflow was removing indentation from code.

Please help as I have it due by tomorrow for semester project

kp9821
  • 13
  • 1
  • 3
  • 2
    your indentation is all screwed up ... thats why it doesnt work ... – Joran Beasley Mar 10 '17 at 00:26
  • Thank you could you help somehow? – kp9821 Mar 10 '17 at 00:31
  • I recommend using a good ide ... something like pycharm ... it will tell you about these problems and suggest fixes – Joran Beasley Mar 10 '17 at 00:37
  • See "[Markdown Help: Code and Preformatted Text](https://stackoverflow.com/editing-help#code)" for help on indenting code blocks. – Kevin J. Chase Mar 10 '17 at 01:24
  • Please (re-)read "[ask]", then [edit] your question to include a [mcve], so that your question will remain useful to others even after that Paste Bin text vanishes. (Note: I suspect this will end up closed as a "[simple typographical error](https://stackoverflow.com/help/on-topic)" (#2), but it might not.) – Kevin J. Chase Mar 10 '17 at 01:37
  • See also: Eric Lippert's "[How To Debug Small Programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)". – Kevin J. Chase Mar 10 '17 at 01:38
  • PS: You don't need Java-style getters and setters in Python. – Kevin J. Chase Mar 10 '17 at 01:42
  • Please post your code inside your question rather than on an external site like pastebin. [edit] your question to include it. Please try to reduce your code to the smallest program that will still reproduce your problem, so that you have a [mcve]. – das-g Mar 11 '17 at 18:16

1 Answers1

1

your code is all fine: it's your indentation that's messed up:

# starting of Employee class
class Employee(object):
    def __init__(self): #declaring Constructor
        self.name = ""
        self.iDnumber = ""
        self.department = ""
        self.jobTitle = ""

    # setter methode for setting values to the class properties
    def setName(self,name):
            self.name=name
    def setIDnumber(self,iDnumber):
            self.iDnumber=iDnumber
    def setDepartment(self,department):
            self.department=department
    def setJobTitle(self,jobTitle):
            self.jobTitle=jobTitle

    # getter methode for getting values of the class properties
    def getName(self):
            return self.name
    def getIDnumber(self):
            return self.iDnumber
    def getDepartment(self):
            return self.department
    def getJobTitle(self):
            return self.jobTitle

    # methode which takes object as an argument and display its properties
    def display(emp_object):
        print("Name : ",emp_object.getName())
        print("IDnumber : ",emp_object.getIDnumber())
        print("Department : ",emp_object.getDepartment())
        print("JobTitle : ",emp_object.getJobTitle())

# Main methode of the program
if __name__ == "__main__":
    employeeList = [] #List to hold the Employee objects
    emp1 = Employee()
    emp2 = Employee()
    emp3 = Employee()

    # appending objects to the list
    employeeList.append(emp1)
    employeeList.append(emp2)
    employeeList.append(emp3)
    input()

    # Initializing each objects of the list
    for employee in employeeList:
        emp_name = input("Enter your Name ")
        employee.setName(emp_name)
        emp_iDnumber = input("Enter your iDnumber ")
        employee.setIDnumber(emp_iDnumber)
        emp_department = input("Enter your Department ")
        employee.setDepartment(emp_department)
        emp_jobTitle = input("Enter your JobTitle ")
        employee.setJobTitle(emp_jobTitle)

    # Displaying each objects of the list
    for emp_object in employeeList:
        display(emp_object)

output:

Enter your Name viki
Enter your iDnumber 1
Enter your Department a
Enter your JobTitle b
Enter your Name viki2
Enter your iDnumber 2
Enter your Department b
Enter your JobTitle c
Enter your Name viki3
Enter your iDnumber 3
Enter your Department a
Enter your JobTitle b
Name :  viki
IDnumber :  1
Department :  a
JobTitle :  b
Name :  viki2
IDnumber :  2
Department :  b
JobTitle :  c
Name :  viki3
IDnumber :  3
Department :  a
JobTitle :  b

notes:

use method names like get_name or set_name instead of setName. you can also use property-decorator : How does the @property decorator work?

PS: Also go through the comments on your questions, There are really good suggestions that will help you improve your coding ability once you are done with your semester.

Community
  • 1
  • 1
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70