Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 2-Mar-19 2:00am
Comments
Richard MacCutchan 2-Mar-19 4:54am    
That code looks wrong, you have the declaration for emp1 inside the class, rather than as part of your main code. Also, if you want a method to update the pay then that should be simple matter, something like:
def update(self, value):
    self.pay += value

1 solution

Try this:
Python
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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900