1

I'm a beginner and i have been trying to get employee leave management data from user input of 'n' employees using class function.

Here are my codes to get n inputs from user for employee leave management

class Employee:
    n=int(raw_input("Pls enter the number of employees:"))

def __init__(self, name, idno, leavetaken, leavereqd, reason):

    self.name=name
    self.idno=idno
    self.leavetaken=leavetaken
    self.leavereqd=leavereqd
    self.reason=reason

    def Employee(self):
        print("name:", self.name,", idno:", self.idno, ", leavetaken:", self.leavetaken,", leavereqd:", self.leavereqd,", reason:", self.reason)

print "Employee.name:", Employee.name             
print "Employee.idno:", Employee.idno
print "Employee.leavetaken:", Employee.leavetaken
print "Employee.leavereqd:", Employee.leavereqd
print "Employee.reason:", Employee.reason
2
  • remove double underscores from name and etc You didnt get an error on __name__ just because it's built-in function which return class name in your case Commented Mar 6, 2017 at 6:04
  • Your class does not have attribute __idno__ (nor idno, for the matter). It has attribute Idno. Same with the other four attributes. As a side note, you attempt to print the name and other attributes of the function Employee, and it does not have any of them aside from __name__, but it is not the name you think it is. Commented Mar 6, 2017 at 6:07

2 Answers 2

1

Are you trying to do something like this?

class Employee:
    def __init__(self, name, idno, leavetaken, leavereqd, reason):
        self.name = name
        self.idno = idno
        self.leavetaken = leavetaken
        self.leavereqd = leavereqd
        self.reason = reason

    def __str__(self):
        return "Name: {}\nID No:{}\nLeave Taken: {}\nLeave Requested: {}\nLeave Reason: {}\n".format(self.name, self.idno, self.leavetaken, self.leavereqd, self.reason)

n = int(raw_input("Please enter the number of employees: "))
employees = []

# capture employee leave requests from input
for i in range(n):
    print("\n({} of {})".format(i + 1, n))
    employee = Employee(
        raw_input("Employee name: ") or "employee".format(i + 1),
        raw_input("Employee ID no: ") or str(i + 1),
        int(raw_input("Employee leave taken: ") or "0"),
        int(raw_input("Employee leave requested: ") or "0"),
        raw_input("Employee leave reason: ") or "No reason"
    )
    employees.append(employee)
print("\n")

# print captured leave requests
print("Employee leave requests:")
for employee in employees:
    print(employee)
Sign up to request clarification or add additional context in comments.

3 Comments

That's the codes i was looking for. Thanks a lot :)
Hpw to write the above codes into a csv file?? sorry. Im a beginner
There are a lot of references out there for beginners pythonforbeginners.com/systems-programming/… Try to write the code first & post another question if you stuck again
0

You are defining "Employee" twice; once as a class and then as a function. When you're trying to access the __name__ attribute of "Employee" you accessing the function's attribute rather than the class's one (that's why the __name__ succeeded while __idno__ fails).

7 Comments

I have made changes to my code above. Still im getting the error Pls enter the number of employees:1 Employee.name: Traceback (most recent call last): File "C:/Python27/emplclass.py", line 15, in <module> print "Employee.name:", Employee.name AttributeError: class Employee has no attribute 'name'
I see your edit. First, your init function should be under the class (one indent in). Second, You are trying to access those attributes (name, idno etc.) as if they were "static" attributes. If you want to access those, first instantiate a new instance of Employee : emp1 = Employe("John", 1, 0, 0, "reason") and then access it's attributes emp1.name or your function emp1.Employee().
I want to get those john 1,0,0,reason as general ones like name, id, reason etc as the no of employees and these info are user inputs so i cant define them before
Ok, so that number of employes input should be outside the class definition (also as best practice). Right above where you defined a new employee, get the input and then iterate as many times as input using for i in range(n) and then just like you've inputted the number of employees just input all the other stuff.
tutorialspoint.com/python/python_classes_objects.htm im trying to make changes to the accessing attributes program by getting user inputs and by adding few more values
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.