-1

This is my Employee class:

class Employee:
     def __init__(self, first, last, pay):
        self.first = first                              # Instance Variable 
        self.last = last                                # Instance Variable 
        self.pay = pay                                  # Instance Variable 
        self.email = first + '.' + last + '@gmail.com'  # Instance Variable 

    def fullname(self):                                  #method inside the class
        return '{} {}'.format(self.first, self.last)

    def apply_raise(self):
        self.pay = int(self.pay * 3 )

emp_1 = Employee('Corey', 'Schafer' , '5000')  

emp_1.apply_raise()
print(emp_1.pay)

I want to get the result 5000 * 3 = 15000 after calling the method apply_raise but instead, I am getting 5000 three times like this:

500050005000
martineau
  • 119,623
  • 25
  • 170
  • 301
Anita
  • 31
  • 3

2 Answers2

2

you should use this:

self.pay = int(self.pay)*3

what you are doing is multiplying a string 3 times, which means putting the string 3 times next to itself!! and then you convert the result to an integer! here's the sequence in which your code will run:

self.pay * 3 -> 500050005000 (as a string)
int(self.pay * 3 ) -> 500050005000 (as an int)


if you want your class to be consistent you should convert the result to a string, because your first self.py was a string:
self.pay = str(int(self.pay)*3)
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
2

You initialize pay as a string by enclosing it in apostrophes.

String * int in python is a valid operation, which repeats string the number of times of int value. Which is what you get in your output.

If you want the pay to be numeric, remove the apostrophes when initializing your object. Then of course the numeric multiplication will happen.

You are trying to convert the result to int , būt you should first ensure that the parameter already is:

self.pay = int(self.pay) * 3 
Gnudiff
  • 4,297
  • 1
  • 24
  • 25