0

This class Employee is supposed to print 50001 when the method of apply_method i called. however it's printed "None". I am tuck please help.

class Employee:
    
    def __init__(self,first,last,pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@mycompany.com'

    def fullname(self):
        return '{} {}'.format(self.first,self.last)

    def apply_method(self):
        self.pay =  int(self.pay + 1) 

empl_1 =Employee('John','Gan',50000)
print(empl_1.apply_method()) 
prosoitos
  • 6,679
  • 5
  • 27
  • 41
Tai Phan
  • 21
  • 3
  • What output were you expecting? Hint: https://stackoverflow.com/questions/10067013/is-it-possible-to-not-return-anything-from-a-function-in-python – AMC Sep 26 '20 at 22:04

3 Answers3

1

It's because you need a return in apply_method

class Employee:
    
    def __init__(self,first,last,pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@mycompany.com'

    def fullname(self):
        return '{} {}'.format(self.first,self.last)

    def apply_method(self):
        self.pay = int(self.pay + 1) 
        return self.pay

empl_1 =Employee('John','Gan',50000)
print(empl_1.apply_method()) 
#or print(empl_1.pay)
RobinFrcd
  • 4,439
  • 4
  • 25
  • 49
1

You don't have return clause in apply_method. Try this;

def apply_method(self):
    self.pay =  int(self.pay + 1)
    return self.pay

If you want to print an output of a function or method, that function or method needs to return something.

malisit
  • 1,253
  • 2
  • 17
  • 36
1

It's because the function is not returning anything. You need to return self.pay-

def apply_method(self):
    self.pay =  int(self.pay + 1)  
    return self.pay
Shradha
  • 2,232
  • 1
  • 14
  • 26