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
asked Mar 6, 2017 at 6:02
anonymous
1431 gold badge2 silver badges16 bronze badges
2 Answers 2
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)
answered Mar 6, 2017 at 6:33
Yohanes Gultom
3,8522 gold badges28 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
anonymous
That's the codes i was looking for. Thanks a lot :)
anonymous
Hpw to write the above codes into a csv file?? sorry. Im a beginner
Yohanes Gultom
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
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).
answered Mar 6, 2017 at 6:14
Den1al
6691 gold badge10 silver badges18 bronze badges
7 Comments
anonymous
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'
Den1al
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().anonymous
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
Den1al
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.anonymous
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
|
lang-py
__name__just because it's built-in function which return class name in your case__idno__(noridno, for the matter). It has attributeIdno. Same with the other four attributes. As a side note, you attempt to print the name and other attributes of the functionEmployee, and it does not have any of them aside from__name__, but it is not the name you think it is.