Class Salesman has 3 attributes: salesman, sales_id, and years_worked.
I would like to define bonus, using the years_worked. I want to add “$” for each year worked.
How do I def bonus()?
The following gives me this error: <function Salesman.bonus at 0x000002602DCAF9C0>
class Salesman:
def __init__ (self, salesman, sales_id, years_worked):
self.salesman = salesman
self.sales_id = sales_id
self.years_worked = years_worked
def __str__(self):
print_me = 'nSalesman: ' + str(self.salesman)
print_me += 'nSales ID: ' + str(self.sales_id)
print_me += 'nAnniversary: ' + str(self.years_worked) + "years"
return print_me
def bonus():
bonus = "$" * self.years_worked
print(salesman, 'earned ', bonus, 'so far this year!')
def main():
print("nWe would like to Congratulate the following Employees", end = " ")
print("who have hit their workiversary")
salesman1 = Salesman("Frank", 1234, 10)
salesman2 = Salesman("Justin", 5678, 5)
print(salesman1)
print(salesman2)
print()salesman1_bonus = Salesman.bonus("Frank", 10)
print(salesman1_bonus)
User1234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Specifically, you need to make sure that the bonus
method has the correct parameters and is called on an instance of the Salesman
class. Here’s how you can do it:
class Salesman:
def __init__(self, salesman, sales_id, years_worked):
self.salesman = salesman
self.sales_id = sales_id
self.years_worked = years_worked
def __str__(self):
print_me = 'nSalesman: ' + str(self.salesman)
print_me += 'nSales ID: ' + str(self.sales_id)
print_me += 'nAnniversary: ' + str(self.years_worked) + " years"
return print_me
def bonus(self):
bonus = "$" * self.years_worked
return f"{self.salesman} earned {bonus} so far this year!"
def main():
print("nWe would like to Congratulate the following Employees", end=" ")
print("who have hit their workiversary:")
salesman1 = Salesman("Frank", 1234, 10)
salesman2 = Salesman("Justin", 5678, 5)
print(salesman1)
print(salesman2)
salesman1_bonus = salesman1.bonus()
salesman2_bonus = salesman2.bonus()
print(salesman1_bonus)
print(salesman2_bonus)
if __name__ == "__main__":
main()
- Add the
self
parameter to thebonus
method so that it can access the instance attributes. - Call the
bonus
method on the instances of theSalesman
class, not on the class itself. - Print the bonus inside the method and return the value if you want to use it later.
Let’s say we define an object called my_remote, then we give method/s to the object(my_remote)like:power_off, power_on, ok, and other methods.If we want to call the method power_off, then, we must write like this:
power_off()
or if we want to do activities, we could also write like this:
my_remote.power_off()
————————-//////——————————–