I want to try write a code where my program will print information of the employee and then there will be another method where I can pass a parameter (integer) and that will not return anything but when I will call the method it will add that integer with one of the instance variable (in my case with self.pay). My problem is I am confused how can I call that method and so I need some help.
My code is:class Information:
def__init__ (self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def __str__(self):
return self.first + self.last + "will get" + str(self.pay)
emp1 = Information("tom","jerry",555666)
def extra(self):
self.bonus = int(self.bonus + self.pay)
calling_method = self.bonus
print (calling_method)
I would really appreciate if someone can help me out. Thank you.
What I have tried:
I have tried to write "emp1" after "extra" method but in that case I got a name error.
解决方案Try this:
class Information:
def __init__ (self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def __str__(self):
return self.first + "" + self.last + "will get " + str(self.pay)
def extra(self, value):
print("adding bonus of ", value)
self.pay += value
emp1 = Information("tom","jerry",555666)
print(emp1)
emp1.extra(111)
print(emp1)