class Employee:

def __init__ (self, name, basic, house):

    self.name = name
    self.basic = basic
    self.house = house 
    self.nhif = (2/100)* self.basic
    self.tax = (30/100) * self.basic

def gross_income (self):
    return (self.basic + self.house)

def total_deductions (self):
    total_deductions = (self.nhif + self.tax)
    return (self.total_deductions)

def net_salary (self):
    net_salary = (self.gross_income - self.total_deductions)
    return  (self.net_salary)

employee1 = Employee ("Peter", 50000, 12000)
employee2 = Employee ("Mary", 60000, 15000)
employee3 = Employee ("Juma", 45000, 12000)

print ("Employee name:", employee1.name, "Gross pay: ", employee1.gross_income(), "Net_salary: ", employee1.net_salary)
print ("Employee name:", employee2.name, "Gross pay: ", employee2.gross_income(), "Net_salary: ", employee2.net_salary)
print ("Employee name:", employee3.name, "Gross pay: ", employee3.gross_income(), "Net_salary: ", employee3.net_salary)

OUTPUT

Employee name: Peter Gross pay: 62000 Net_salary: <bound method Employee.net_salary of <main.Employee object at 0x0000021AD3505D60>>
Employee name: Mary Gross pay: 75000 Net_salary: <bound method Employee.net_salary of <main.Employee object at 0x0000021AD35050D0>>
Employee name: Juma Gross pay: 57000 Net_salary: <bound method Employee.net_salary of <main.Employee object at 0x0000021AD33F8970>>

Recommended Answers

All 2 Replies

You are using things both as functions and variables. Compare the following with your code

class Employee:

    def __init__ (self, name, basic, house):

        self.name = name
        self.basic = basic
        self.house = house 
        self.nhif = (2/100)* self.basic
        self.tax = (30/100) * self.basic

    def gross_income (self):
        return self.basic + self.house

    def total_deductions (self):
        return self.nhif + self.tax

    def net_salary (self):
        return self.gross_income() - self.total_deductions()


employee1 = Employee ("Peter", 50000, 12000) 
employee2 = Employee ("Mary", 60000, 15000)
employee3 = Employee ("Juma", 45000, 12000)


print ("Employee name:", employee1.name, "Gross pay: ", employee1.gross_income(),"Net_salary: ", employee1.net_salary())
print ("Employee name:", employee2.name, "Gross pay: ", employee2.gross_income(),"Net_salary: ", employee2.net_salary())
print ("Employee name:", employee3.name, "Gross pay: ", employee3.gross_income(),"Net_salary: ", employee3.net_salary())

I suggest you have a look at lists as the following illustrates:

employees = [Employee ("Peter", 50000, 12000),
             Employee ("Mary", 60000, 15000),
             Employee ("Juma", 45000, 12000),
            ]

for employee in employees:
    print ("Employee name:", employee.name,
           "Gross pay: ", employee.gross_income(),
           "Net_salary: ", employee.net_salary()
           )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.