I NEED HELP URGERNTLY BEFORE I LOSE INTEREST

General discussion on technical and managerial topics
no avatar
cocman
 
Posts: 2
Joined: Wed Jul 27, 2011 10:25 pm

I NEED HELP URGERNTLY BEFORE I LOSE INTEREST

by cocman » Wed Jul 27, 2011 10:36 pm

I an new to programming, I progressed to oop in python using different books but when I get to oop things go very wrong. then I went on-line but but I still get errors when I run an example code which ran perfectly on their system. For example this code I found on tutorialspoint:


class Employee:
'common base class for all employees'
empCount=0
def displayCount(self):
print "Total employee %d"%Employee.empCount

def displayEmployee(self):
print "Name:",self.name,'salary:',self.salary

emp1= Employee()
emp2= Employee()
emp1.displayEmployee()
emp2.displayEmployee()
print "Total employee(s)%d"%employee.empCount

when they ran it it printed on the screen this:

Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2


but if I run the same code I get this:

>>>
Name:
Traceback (most recent call last):
File "J:\employee.py", line 20, in <module>
emp1.displayEmployee()
File "J:\employee.py", line 14, in displayEmployee
print "Name:",self.name,'salary:',self.salary
AttributeError: Employee instance has no attribute 'name'

this same error is produced any time I run any python code on oop
I mean is OOP not for everyone?
because I am sick and tired of this rubbish

no avatar
tutorialspoint
Site Admin
 
Posts: 144
Joined: Fri Apr 27, 2007 6:52 pm

by tutorialspoint » Thu Jul 28, 2011 4:07 am

I think you are using different python version. I'm using Python 2.4.3 and it is working fine. Just check the syntax with new version.
Moderator, TP
Keep visiting and share this site with your friends.

no avatar
cocman
 
Posts: 2
Joined: Wed Jul 27, 2011 10:25 pm

»I made a big mistake in the code I posted

by cocman » Thu Jul 28, 2011 4:15 pm

I made a mistake that is a long story. This is the real code
class Employee:
'common base class for all employees'

empCount=0

def _init_(self,name,salary):
self.name = name
self. salary = salary
Emloyee.empCount +=1

def displayCount(self):
print "Total employee %d"%Employee.empCount

def displayEmployee(self):
print "Name :", self.name, "salary : ", self.salary




"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount


tuorials point result:

Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2

my result:

Traceback (most recent call last):
File "J:\employee.py", line 21, in <module>
emp1 = Employee("Zara", 2000)
TypeError: this constructor takes no arguments[/code]

Return to General Discussion

cron