I see two issues in the example you posted:
- The getWeeksPay function needs to be indented so that it is interpreted as a PersonWorker class method versus a normal function.
- You have some funky characters at the end of the return statement in the str method.
I updated your code snippet with an example of what I think you are trying to accomplish.
class PersonWorker:
def __init__(self, firstName, lastName, phoneNo, rate=0):
self.firstName= firstName
self.lastName= lastName
self.phoneNo= phoneNo
self.rate= rate
def getFirstName(self):
return self.firstName
def getLastName(self):
return self.lastName
def getPhoneNo(self):
return self.phoneNo
def getWeeksPay(self,hours):
if rate is 0: raise Exception("Rate not set")
return hours*self.rate
def __str__(self):
stringRep = "First Name: " + self.firstName + "\n"
stringRep += "Last Name: " + self.lastName + "\n"
stringRep += "Phone Number : " + self.phoneNo + "\n"
return stringRep