I have the below piece that created few person objects and apply some methods on those objects.
class Person:
def __init__(self, name, age, pay=0, job=None):
self.name = name
self.age = age
self.pay = pay
self.job = job
def lastname(self):
return self.name.split()[-1]
def giveraise(self,percent):
return self.pay *= (1.0 + percent)
if __name__ == '__main__':
bob = Person('Bob Smith', 40, 30000, 'software')
sue = Person('Sue Jones', 30, 40000, 'hardware')
people = [bob,sue]
print(bob.lastname())
print(sue.giveraise(.10))
Once I run this program, this is the output--
Syntax Error: Invalid syntax
but when I run using the below code, I don't have any problem,
if __name__ == '__main__':
bob = Person('Bob Smith', 40, 30000, 'software')
sue = Person('Sue Jones', 30, 40000, 'hardware')
people = [bob,sue]
print(bob.lastname())
sue.giveraise(.10)
print(sue.pay)
What is the difference in two cases
giveraise()shouldn't return a value. "Give raise" implies you are acting on the data in the class instance. It would make more sense to have a separate property accessor for post-raise pay.