Python Referencing a name of a class object in a function -


i've never used classes before , trying general understanding of how work code example have below. im having issues referencing 1 of names define class. want program print out list of employee names , salaries stored in list when option 2 entered gives me following error:

traceback (most recent call last): file "c:\scott glenn\misc\classes.py", line 31, in employees[i].displayemployee attributeerror: 'str' object has no attribute 'displayemployee'

class employee:     'common base class employees'     empcount = 0      def __init__(self, name, salary):         self.name = name         self.salary = salary         employee.empcount += 1      def displaycount(self):         print "total employee %d" % employee.empcount      def displayemployee(self):         print "name : ", self.name,  ", salary: ", self.salary  def addnewemployee():     newemployee = raw_input("what employees name: ")     employees.append(str(newemployee))     newemployeesalary = raw_input("what employees salary: ")     newemployee = employee(newemployee, newemployeesalary)     return employees #============================================================================= employees=[] while(1):     print'welcome employee database!'     option = raw_input('please select 1 add new employee or 2      display current employees: ')     if option=='1':         employees.append(addnewemployee())     if option=='2':         in range(0,len(employees)):             employees[i].displayemployee 

the addnewemployee function wrong. it's returning list of single string when want returning single object of custom type employee.

it should more this:

def addnewemployee():      #string variable hold name     newemployeename = raw_input("what employees name: ")      #why make list? appending result of function list     #employees.append(str(newemployee))     #plus adding employee before he's been created      newemployeesalary = raw_input("what employees salary: ")      #construct using name string , salary string     newemployee = employee(newemployeename, newemployeesalary)       return newemployee #return employee object (to appended later) 

additionally, trying access displayemployee() field of class, instead of method. fields don't have parenthesis , methods (so can take parameters, though in case parenthesis empty no parameters passed).

finally, note raw_input returns string should cast float if wish newemployeesalary be. (right it's string.)


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -